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
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
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
4
Q
What is the time complexity?
A
O(n)
- Traverse through nums array only once
5
Q
What is the space complexity?
A
O(n)
- Due to hashmap usage