Two Sum Flashcards

(17 cards)

1
Q

What is the classic problem discussed in the text?

A

Two Sum

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

List keywords that should stand out when recognizing the Two Sum problem.

A
  • return indices
  • two numbers
  • add up to target
  • exactly one solution
  • don’t use the same element twice
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What type of problem is the Two Sum problem categorized as?

A

Hashing problem

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

What are the constraints you can assume in the Two Sum problem?

A
  • There is always exactly one solution
  • You can return the indices in any order
  • Don’t reuse the same index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the goal of the Two Sum problem?

A

Find two numbers in the array that sum to the target and return their indices.

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

Describe the brute force approach to solving the Two Sum problem.

A

For each element, check every other element to see if the sum equals the target.

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

What is the time complexity of the brute force approach?

A

O(n²)

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

What is the best approach to optimize the Two Sum problem?

A

Using a hash map

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

What is the core idea of the hash map approach?

A

Store each number in a hash map with its index and check if the complement exists.

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

What is the time complexity of the optimized hash map approach?

A

O(n)

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

What is the space complexity of the optimized hash map approach?

A

O(n)

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

What should you clarify in an interview when discussing the Two Sum problem?

A
  • Can I assume there’s only one solution?
  • Can the same number be used twice?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What should you do after quickly discussing the brute force method?

A

Propose the hash map approach.

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

In the hash map approach, when should you add the number to the hash map?

A

After checking the complement to avoid using the same element twice.

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

Provide an example of how the Two Sum algorithm works with nums = [2, 7, 11, 15] and target = 9.

A
  • Iteration 0: num = 2, complement = 7 → not in seen, store 2:0
  • Iteration 1: num = 7, complement = 2 → found! return [0, 1]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the summary of approaches for the Two Sum problem?

A
  • Brute Force: O(n²), O(1)
  • Hash Map: O(n), O(n)
17
Q

What problem is this?
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.