Indeed Technial Flashcards

1
Q

Subdomain Visit Count

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Find Words That Can Be Formed by Characters

A

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
    • Add the temp to result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Shortest Word Distance

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Moving Average from Data Stream

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

String Matching in an Array

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Binary Tree Pruning

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Kth Largest / Smallest Element in an Array

(Heap)

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Excel Sheet Column Number

A

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)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Remove Duplicate Letters in Lexicon Order

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Intersection of Two Arrays

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

First Unique Character in a String

A

Run Time:

O(N) Time, O(1) Space

Pattern:

  • Create a counter object
  • Find first frequency = 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Longest Substring Without Repeating Characters

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Repeated String Match

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Find All Anagrams in a String

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Maximum Length of Repeated Subarray

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Minimum Cost Tree From Leaf Values

A

Run Time:

O(N) Time, O(log N) Space

Pattern:

  • Set a result = 0
  • Loop while there is still one element left.
    • We find the min of the arr, since we want to find the minimum product
    • Find the second min in either left or right of the minIdx
    • Corner Case: if minIdx is first or last, times the first or second last.
    • Pop the min ele from arr