Skip to content

Instantly share code, notes, and snippets.

@habina
habina / CareerCup 1.1.py
Last active August 29, 2015 14:02
1.1 Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures?
def uniqueString(str):
"Return True if the string has all unique characters."
allChar = []
for i in range(256):
allChar.append(True)
for char in str:
if(allChar[ord(char)]):
allChar[ord(char)] = False
else:
return False
@habina
habina / CareerCup 1.2.c
Last active February 28, 2022 14:58
1.2 Implement a function void reverse(char* str) in C or C++ which reverses a null-terminated string.
#include <stdio.h>
void swap(char* a, char* b);
void reverse(char* str);
int main(){
char strArray[] = "1point3acres";
printf("Before Reverse: %s\n", strArray);
reverse(strArray);
printf("After Reverse: %s\n", strArray);
}
@habina
habina / CareerCup 1.3.py
Last active August 29, 2015 14:02
1.3 Given two strings, write a method to decide if one is a permutation of the other.
def permutationString(firstStr, secondStr):
"Return True if the second string is a permutation of the other"
"""If the length of two strings are different,
directly return false
"""
if(len(firstStr) != len(secondStr)):
return False
"Change all characters to lowercase"
@habina
habina / CareerCup 1.4.py
Last active August 29, 2015 14:02
1.4 Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the additional characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
def replaceSpace(string):
"""Replace all spaces in a string with '%20'"""
"Split string by space, save into a list"
newList = string.split()
"str.join concatenate list at the end"
newString = "%20".join(newList)
return newString
@habina
habina / CareerCup 1.5.py
Last active August 29, 2015 14:02
1.5 Implement a method to perform basic string compression using the counts of repeated characters. For example, the string aabcccccaaa would become a2blc5a3. If the "compressed" string would not become smaller than the original string, your method should return the original string.
def stringCompression(originalString):
"""Compress string by using the counts of repeated characters
Input: aabcccccaaa
Output: a2b1c5a3
If the length of string bigger than the compressed string
Then return the original string
"""
if(originalString == ""):
return originalString
@habina
habina / CareerCup 1.6.py
Last active August 29, 2015 14:02
1.6 Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees. Can you do this in place?
def getIndex(x, y, width):
"""
Return the index in 1D array
"""
index = x + y * width
return index
def printMatrix(matrix, width):
"""
Print Matrix row by row
@habina
habina / CareerCup 1.7.py
Last active August 29, 2015 14:02
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.
def getIndex(x, y, width):
return x + y * width
def printMatrix(matrix, m, n):
"""
Print Matrix row by row
"""
for i in range(m*n):
print(matrix[i], end="\t")
if((i + 1) % n == 0):
"""
============================================================================
Question :Write code to remove duplicates from an unsorted linked list.
FOLLOW UP
How would you solve this problem if a temporary buffer is not allowed?
Solution : iterate through the whole, add char to a set
if char already existed in set, skip this node
Time Complexity : Average Case: O(1), Worst Case: O(N)
Space Complexity: O(N)
Gist Link : https://gist.github.com/habina/3896531754d629b2032f
@habina
habina / CareerCup2.2.py
Last active August 29, 2015 14:03
2.2 Implement an algorithm to find the kth to last element of a singly linked list.
"""
============================================================================
Question : 2.2 Implement an algorithm to find the kth to last element of a singly linked list.
Solution : Faster pointer move forward Kth node first, then move forward to the end alone with slow pointer,
until faster pointer reaches the end, return slower pointer
Time Complexity : O(N)
Space Complexity: O(1)
Gist Link : https://gist.github.com/habina/f521508cef301ac0d18d
============================================================================
"""
"""
============================================================================
Question : Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
EXAMPLE
Input: the node c from the linked list a->b->c->d->e
Result: nothing isreturned, but the new linked list looks like a- >b- >d->e
Solution : Copy the following pointer content, and relink the next to the next next pointer
Time Complexity : O(1)
Space Complexity: O(1)
Gist Link : https://gist.github.com/habina/1effe686ec65dbadf1ec