Algo Components Flashcards

1
Q

if not array:
return 0 # or [], None, etc.

A

Empty input guard - Protects against empty array cases

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

if len(array) == 1:
return array[0]

A

Single element guard - Handles single element edge case

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

if start > end:
return -1

A

Invalid input guard - Protects against invalid range inputs

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

current_max = float(‘-inf’)
current_min = float(‘inf’)

A

Min/Max tracking - Initialize tracking variables for finding extremes

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

prev = array[0]
for current in array[1:]:
# compare with prev

A

Previous value tracking - Pattern for comparing consecutive elements

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

running_sum = 0
prefix_sums = [0] # Include 0 for easier calculations

A

Running sums - Track cumulative sums for efficient range calculations

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

window_sum = sum(array[:k])
for i in range(k, len(array)):
# slide window

A

Fixed size window - Initialize and slide a window of constant size

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

start = 0
current_sum = 0
for end in range(len(array)):
# expand window
while condition_broken:
# contract window

A

Dynamic window - Window that can grow and shrink based on conditions

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

left, right = 0, len(array) - 1
n = len(array)

A

From Ends Setup - Initialize pointers at both ends of array

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

slow = fast = start_point
seen = set()

A

Fast/Slow Setup - Initialize pointers for cycle detection or similar patterns

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

write = 0
read = 1

A

Same Direction Setup - Initialize pointers for in-place array modification

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

for i in range(len(array)):
# process until end

A

Bounded loop - Process array with known bounds

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

while left < right:
# process until condition met

A

Conditional loop - Process until pointers meet or condition satisfied

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

while slow and fast and fast.next:
# process with multiple conditions

A

Multi-pointer loop - Process with multiple pointer conditions

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

def modify_array(array):
write_idx = 0
for read_idx in range(len(array)):
# modify array directly

A

In-place modification - Modify array without extra space

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

def build_result(array):
result = []
for element in array:
# append to result
return result

A

New result construction - Build new result structure

17
Q

if d and d[0] < i - k + 1:
d.popleft()
d.append(i)

A

Creates sliding window in a deque of k elements. This removes the leftmost element.