Indeed Technial Flashcards
Subdomain Visit Count
Runtime
Time Complexity: O(N)O(N), where NN is the length of cpdomains, and assuming the length of cpdomains[i] is fixed.
Space Complexity: O(N)O(N), the space used in our count.
Pattern
- Create a hash table
- loop through domains, and split count and domain.
- Recursively call a helper function that stores the count and breaks down the subdomain.
- Return domain after each call
- Print out domain into a list
Find Words That Can Be Formed by Characters
Run Time:
- O(N^2)
Pattern:
- Create a counter of chars and result
- Loop through each word
- Make a copy of the counter of chars (we will be deleting elements and need original every time)
- Set a temp to add
- Loop through the char in word
- If the char is in counter, and the char in hashtable > 0
- Decrement char
- increase temp
- else reset temp and break
- If the char is in counter, and the char in hashtable > 0
- Add the temp to result
Shortest Word Distance
Run Time
O(N * M) Time, O(1) Space
Pattern
- Create two idxs for word1 and word2, and minDistance = len(words)
- Loop through words, and check if and words equal word1 or word2
- Update index if so.
- Compare the minDistance if word1 or word2 ≠ -1
Moving Average from Data Stream
Run Time:
O(N) time, O(N) space
Pattern:
- Create a list as a storage within the constructor
- Within the next() method, append the val to the list
- Pop the list if the list is greater than the length indicated or that the len is not 0.
- return the float average of sum(list)/len(list)
String Matching in an Array
Run Time
O(N^2) time, O(N) space
Pattern
- Sort through the words, since you smaller fits into bigger.
- Loop through words and a second loop starting at I + 1
- If word[I] is in word[j], append and break
Binary Tree Pruning
Run Time:
O(N) Space, O(N) Time
Pattern:
- Recursive base case for trees
- Recurse to the bottom of the tree first
- Set node.left and right = recursive call
- Condition: if node left and right are None and val = 0, return None
- else return root
Kth Largest / Smallest Element in an Array
(Heap)
Run Time:
- O(N log k) Time, and O(N) space
- Adding to heaps are O(log k)
Pattern
heapq. heappush(heap, item): Push the value item onto the heap
heapq. heappop(heap): Pop and return the smallest item from the heap. To access the smallest item without popping it, use heap[0].
heapq. heappushpop(heap, item): Push item on the heap, then pop and return the smallest item from the heap.
heapq. heapreplace(heap, item): Pop and return the smallest item from the heap, and also push the new item. The heap size doesn’t change.
heapq. heapify(x): Transform list x into a heap, in-place, in linear time.
heapq. merge(*iterables, key=None, reverse=False) Merge multiple sorted inputs into a single sorted output.
heapq. nlargest(n, iterable, key=None) Return a list with the n largest elements from the dataset defined by iterable.
heapq. nsmallest(n, iterable, key=None): Return a list with the n smallest elements from the dataset defined by iterable.
Excel Sheet Column Number
RunTime
O(N) time, O(1) Space
Pattern
- Create an alphabet dict
- {char(I + 65} : i + 1 for I in range(26)}
- Set a result, and decimal to 0
- Starting from the right to left, since thats how numerical base works
- Get the idx from alphabet dict
- The idx * 26 (number of options)^power of decimal)
Remove Duplicate Letters in Lexicon Order
Run Time:
O(N) Time, O(1) Space
Pattern:
- Create a dictionary with last seen
- Create a stack to keep track of last seen letters
- If the letter is not in stack:
- Loop and pop while stack is not empty, top of stack is greater than char, and last seen of top of stack is greater than idx
- Append the new letter
- Return joined list
Intersection of Two Arrays
Run Time:
O(N + M) time, O(N + M)
Pattern:
- Make both lists into sets
- return using list comprehension if one set is in another
First Unique Character in a String
Run Time:
O(N) Time, O(1) Space
Pattern:
- Create a counter object
- Find first frequency = 1
Longest Substring Without Repeating Characters
Run Time:
O(N) Time, O(N) Space
Pattern:
- Create a dictionary to keep track of last idx location, and left idx, result
- Loop through right
- If it not in seen, increment result (right - left + 1)
- else:
- If it is outside window, we keep incrementing
- If it is inside window, we set the left to last seen idx
- increment the last seen in dict
Repeated String Match
Run Time:
O(N) time, O(N) space
Pattern:
- Set a temp string and a count
- Loop while the length of temp is less than both A and B.
- Add a copy of temp and increment count
- See if b is in temp, if so return count
- else return -1
Find All Anagrams in a String
Run Time:
O(N) time, O(N) space
Pattern:
- Create two counters, one for pattern, other for length of pattern
- Create I, J for sliding window
- Loop while J is smaller or equal to len(s)
- If the two counters equal, add result
- Decrement the count for tail char
- Deletes tail count if it is a 0
- Move the counter forward
- increment I, J
Maximum Length of Repeated Subarray
Run Time:
O(N) Time, O(N) Space
Pattern:
- convert one of the lists into strings, making it faster to compare
- create a window and result null
- loop through nums1
- Add the num1 into window
- if window is in max_str, then compare and get max
- else: shift window right