AlgoExpert Medium Flashcards

1
Q

Three Number Sum

A

O(N^2) Time | O(N) Space

Pattern:

  • Sort the array, so that you know which side to incre/decrement
  • Loop through array:
    • Set left to i+1, right to last.
    • If equal, append result, and incre/decrement
    • If smaller increment left, if bigger decrement right
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Smallest Difference

A

O(n log n) time | O(1) space

Pattern:

  • sort the two arrays, create two idx, smallestPair, smallestDifference, currDiff
  • Loop while both idx is smaller than len
    • get both numbers
    • If one number is larger than the other, make it the currDiff, increment the idx for smaller number
    • Check if currDiff is smaller than smallest. If it is update smallest and smallest Pair
  • Return smallest Pair
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Move Element To End

A

O(N) Time | O(1) Space

Pattern:

  • Set two idx, one at start, one at end of array.
  • Loop while left and right:
    • Loop while right == target and left < right
    • If left is toMove, swap it with right (since it is not target)
    • Increment left
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Monotonic Array

A

O(N) space | O(1) time

Pattern:

  • If they fluctuate between increasing and decreasing then they are neither strictly increasing or decreasing
  • So set strict too boolean:
  • loop from idx 1:
    • Check if previous idx is < than previous
      • decreasing = False
    • Check if previous idx is > than previous
      • increasing = False
  • return either if true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Longest Peak

A

O(N) Time | O(1) space

Pattern:

  • Set a variable for longestPeak and for I = 1
  • Loop while I is less than second last:
    • find peak by seeing if leftidx or rightidx is decreasing
    • If not continue, increment i
    • else, loop through left side and right side, by setting left idx 2 away and right idx 2 away
      • check if they are strictly decreasing
    • Get the peak length by minus right to left - 1
    • Update longest
    • set i to rightIdx
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Array of Products

A

O(N) time | O(N) Space

Pattern:

  • create a products array with all 1s
  • create a running left product
  • loop incrementally, make product[i] = running product, multiply the running product with array[i]
  • create a running right product
  • loop incrementally, make product[i] multiply by the running product, multiply the running product with array[i]
  • return products
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

First Duplicate Value

A

O(N) time | O(N) space

Pattern:

  • Create a set as seen
  • Loop through the array
    • If the num is in seen, return it
    • add num into seen
  • return -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Spiral Traverse

A

O(N) Time | O(N) Space

Pattern:

  • [Row] [Col]
  • Set four pointers at each corner of the matrix
  • Loop while opposite pointers are smaller or equal to each other
    • loop through col, left to right, add result
    • loop through row, up to down, add result (skips starting, to avoid double count
    • Reverse loop through col, right to left, add result (skips ending)
      • breaks if startrow = endrow to avoid odd counts
    • Reverse loop through col, down to up, add result (skips starting)
      • breaks if startCol = endCol to avoid odd counts
    • incre/decrement based on pointers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Find Kth Largest Value in BST

A

O(N) Time | O(N) Space

Pattern:

  • Since BST is left < right for every node,
  • InorderTravse and append into an array
  • return array[len(array)-kth]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Merge Overlapping Intervals

A

O(N log N) Time | O(N) Space

Pattern:

  • Sort the intervals
  • Have a currentInterval, since it is an array, it is mutable
  • Loop through sortedIntervals:
    • If currentEnd ≥ nextStart, then set currentInterval to nextEnd
    • else: set currentInterval to next interval and add interval to result
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Validate BST

A

O(N) time | O(Depth) space

Pattern:

  • Use a helper function that sets minValue and maxValue to their infinities.
  • Recursively traverse using helper function:
    • If the node is none, return True
    • if the node value is smaller than minValue return false
    • if the node value is greater or equal to maxValue return false
    • traverse left node with current value as new maxValue
    • traverse right node with current value as new minValue
    • return true if both are true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Min Height BST

A

O(N) Time | O(N) Space

Pattern:

  • Create a helper function for recursive call
  • Base condition: if right < left
    • Find mid index of array
    • Create BST node with mid index
    • Recursively call BST node left and right with helper
    • Return BST node
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

BST Construction: Insert()

A

O(N) Time | O(1) Space

Pattern:

  • Set the currentNode to self
  • Loop:
    • If value is smaller / larger,
      • If left / right is none:
        • new BST node
      • If left / right is not none
        • set currentNode to left
  • return BST
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

BST Construction: Contains()

A

O(N) Time | O(1) Space

Pattern:

  • set currentNode to the node
  • Loop while currentNode is not None:
    • If smaller or larger, set currentNode to left or right
    • If they equal return True
  • Else return false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

BST Construction: Remove()

A

O(N) Time | O(1) Space

Pattern:

  • Also use a getMin to find the smallest
  • Use a parent node for same depth swapping
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Invert Binary Tree

A

O(N) Time | O(Depth) Time

Pattern:

  • Base Case: if tree is none, return
  • Swap left and right
  • Recursively call left and right
17
Q

Valid IP Address

A

O(N) Time | O (N) Space

Pattern:

  • Split IP address into four parts, and traverse each part individually
  • Traverse current index up to the min(len(str) or 4), for other traversals add prevIdx and up to min(len(str)-prevIdx, 4)
  • Use a helper function compare if it is valid, if it is go to next part, and if it is second last part, append it to the result array.