Why this pattern for this problem? Flashcards

(30 cards)

1
Q

What is the preferred pattern for solving the Two Sum problem?

A

HashMap

This allows O(1) lookup to check if the complement exists.

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

What is the time complexity of the brute force approach for the Two Sum problem?

A

O(n²)

This involves checking every pair of numbers.

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

What is the preferred pattern for finding the length of the longest substring without repeating characters?

A

Sliding Window + Set

This allows for O(n) time complexity.

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

What is the time complexity of checking every substring for the longest substring without repeating characters?

A

O(n²)

This is inefficient compared to the sliding window approach.

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

What is the preferred pattern for checking if a string of brackets is properly balanced?

A

Stack

This follows Last-In-First-Out logic.

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

What is the time complexity for using a stack to check for balanced parentheses?

A

O(n)

This allows for efficient processing of brackets.

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

What is the preferred pattern for finding the maximum profit from buying and selling stock once?

A

Single Pass Tracking (greedy)

This allows for O(n) time complexity.

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

What is the time complexity of checking all buy/sell pairs for stock trading?

A

O(n²)

This is inefficient compared to the greedy approach.

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

What is the preferred pattern for finding the contiguous subarray with the largest sum?

A

Kadane’s Algorithm (Dynamic Programming)

This allows for O(n) time complexity.

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

What is the time complexity of checking all subarrays for the maximum subarray problem?

A

O(n²) or O(n³)

This is inefficient compared to Kadane’s Algorithm.

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

What is the preferred pattern for merging all overlapping intervals?

A

Sort + Greedy

This allows for O(n log n) time complexity.

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

What is the time complexity of merging intervals without sorting?

A

O(n²)

This is inefficient compared to the sorting approach.

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

What is the preferred pattern for visiting tree nodes level by level?

A

BFS with Queue

This ensures FIFO processing of nodes.

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

What is the time complexity of using DFS for level order traversal?

A

O(n)

BFS is more intuitive for level order.

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

What is the preferred pattern for counting the number of ways to climb stairs?

A

Dynamic Programming (Bottom-Up)

This builds from the bottom up.

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

What is the time complexity of a recursive approach without memoization for climbing stairs?

A

O(2^n)

This leads to recalculating the same stairs.

17
Q

What is the preferred pattern for calculating the product of an array except self?

A

Prefix/Suffix Products

This avoids division and handles zeros.

18
Q

What is the time complexity of using nested loops to calculate the product for each position?

A

O(n²)

This is inefficient compared to the prefix/suffix approach.

19
Q

What is the preferred pattern for grouping anagrams?

A

HashMap with Sorted String as Key

This allows consistent representation of anagrams.

20
Q

What is the time complexity of comparing every pair to check if they are anagrams?

A

O(n² × k)

This is inefficient compared to using a sorted string.

21
Q

What is the preferred pattern for finding the K most frequent elements in an array?

A

HashMap (count) + Heap

This efficiently counts and retrieves top K elements.

22
Q

What is the time complexity of sorting all frequencies to find the top K elements?

A

O(n log n)

Using a heap is more efficient.

23
Q

What is the preferred pattern for counting connected components of 1s in a 2D grid?

A

DFS or BFS from each unvisited land cell

This explores entire islands.

24
Q

What is the time complexity of checking adjacency for each cell without DFS/BFS?

A

O(n²m²)

This is inefficient compared to DFS/BFS.

25
What is the **preferred pattern** for solving the Coin Change problem?
Dynamic Programming (Bottom-Up) ## Footnote This builds up from amount 0 to target.
26
What is the time complexity of a greedy approach for the Coin Change problem?
O(n) ## Footnote This can fail in certain cases.
27
What is the **preferred pattern** for finding two lines that form a container with the most water?
Two Pointers (opposite ends) ## Footnote This allows for O(n) single pass.
28
What is the time complexity of checking all pairs of lines for the container with most water?
O(n²) ## Footnote This is inefficient compared to the two pointers approach.
29
What is the **preferred pattern** for finding all triplets that sum to zero?
Sort + Two Pointers (with outer loop) ## Footnote This simplifies duplicate handling.
30
What is the time complexity of using three nested loops to find triplets that sum to zero?
O(n³) ## Footnote This is inefficient compared to the two pointers approach.