Algorithms Flashcards

1
Q

Describe the tow pointers algorithm where the pointers start at the ends of the array

A
def function(arr):
  left = 0
  right = len(arr) - 1

  while left < right:
    # Do some logic depengin on the problem
    # Do some more logic to decide one of the following
    # 1. left += 1
    # 2. right -= 1
    # 3. left += 1 and right -= 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the two pointers algorighm where there are multiple arrays

A

Note that only one of these loops will run

def function(arr1, arr2):
  i = j = 0
  while i < len(arr1) and j < len(arr2):
    # Do some logic depending on the problem
    # Do some more logic to decide one of the following
    # 1. i += 1
    # 2. j += 1
    # 3. i += 1 and j += 1

Make sure both iterables are exhausted

#Only one of these loops will run
while i < len(arr1):
  # Do logic depending on the problem
  i += 1

while j < len(arr2):
  # Do logic depending on the problem
  j += 1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Describe the sliding window algorithm

A
def function(self, arr: List[int]) -> int:
    left = right = 0

    for right in range(len(arr)):
        
        # Do some logic to "add" elements at arr[right]
        while <windows_is_invalid>:
            # Do logic to "remove" element at arr[left]
            left += 1

Time complexity O(n). The inner loop has an amortized time complexity of O(1)

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

Describe the sliding window algorithm with a fixed window

A
def fixed_window(self, arr, k) -> int:
    sum = # some data to track
    
    # Build first window
    for i in range(k):
        # logic to build first window
    
    ans = # answer variable

    # Build next windows
    for i in range(k, len(nums)):
        # add arr[i] to window
        # remove arr[i-k] from window
        # update answer
    
    return ans
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Describe the prefix sum algorithm

A
def prefix_sum(nums: List[int]) -> List[int]:
    prefix = [nums[0]]
    for i in range(1, len(nums)):
        prefix.append(nums[i] + prefix[i-1])
    
    return prefix

The sum between index i and j is
prefix[j] - prefix[i-1]
or
prefix[j] - prefix[i] + nums[i]

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

Describe an algorithm to build a string

A
def build_strings(s):
    arr = []
    for c in s:
        arr.append(c)

    return "".join(arr)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Describe an algorithm to count the number of subarrays with an “exact” constraint

A
from collections import defaultdict

def solution(nums: List[int], k: int) -> int:
    count = defaultdict(int)
    count[0] = 1
    ans = curr = 0

    for num in nums:
        curr += num
        ans += count[curr - k]
        count[curr] += 1

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

Describe an algorithm to return the value in a middle of a linked list

A
def get_middle(head):
    slow = head
    fast = head

    while slow and fast.next:
        slow = slow.next
        fast = fast.next.next

    return slow.val
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe an algorithm to find cycles in a linked list

A
def cycle_exists(head) -> bool:
    slow = head
    fast = head

    while slow and fast.next:
        slow = slow.next
        fast = fast.next.next
        if slow == fast:
            return True

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

Describe an algorithm to reverse a linked list

A
def reverse_linked_list(head):
    curr = head
    prev = None

    while curr:
        next_node = curr.next
        curr.next = prev
        prev = curr
        curr = next_node

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

Describe an algorithm to build/maintain a monotonic increasing stack

A
def monotonic_inc_stack(nums: List[int]) -> List[int]:
    stack = []

    for num in nums:
        while stack and stack[-1] > num:
            stack.pop()
        stack.append(num)
    return stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe and algorithm to perform Depth First Search (DFS) on a binary tree

A
def dfs(node):
    if node == None:
        return

    dfs(node.left)
    dfs(node.right)
    return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe and algorithm to perform preorder Depth First Search (DFS) on a binary tree

A
def preorder_dfs(node):
    if node == None:
        return

    print(node.value)
    preorder_dfs(node.left)
    preorder_dfs(node.right)
    return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe and algorithm to perform inorder Depth First Search (DFS) on a binary tree

A
def inorder_dfs(node):
    if node == None:
        return

    inorder_dfs(node.left)
    print(node.value)
    inorder_dfs(node.right)
    return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe and algorithm to perform postorder Depth First Search (DFS) on a binary tree

A
def postorder_dfs(node):
    if node == None:
        return

    postorder_dfs(node.left)
    postorder_dfs(node.right)
    print(node.value)
    return
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
A