Two Sum Flashcards

https://leetcode.com/problems/two-sum/?envType=list&envId=rkzwnm95

1
Q

What are the inputs and outputs?

A

input: array of numbers & target number
output: array of 2 index’s that would add up to target

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

What data structure do you plan to use?

A

Javascript’s map object that holds key-value pairs and remembers original insertion order

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

Can you share your approach?

A
  • Loop through each number
  • Create variable to check complement
  • if complement is found in map, then return current index and saved index from map key
  • set map with current number and index
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the time complexity?

A

O(n)
- Traverse through nums array only once

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

What is the space complexity?

A

O(n)
- Due to hashmap usage

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