Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
HAPPY CASE
Input: nums = [1,2,3,1]
Output: true
Explanation: There are two 1 found in this array of numbers.
Input: nums = [1,2,3,4]
Output: false
Explanation: There are no duplicates found in this array of numbers.
EDGE CASE
Input: head = [1]
Output: false
Explanation: There are no duplicates found in this array of numbers
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Array problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Create a Set to store number. If the number is already in the Set, then return True. Otherwise we reach the end of the array and return False.
1) Create Set
2) Iterate through numbers
a) If number is already in set return True
b) Store number in set
3) Return False (we have reached the end of the list without duplicate)
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# Create Set
hashSet = set()
# Iterate through numbers
for num in nums:
# If number is already in set return True
if num in hashSet:
return True
# Store number in set
hashSet.add(num)
# Return False (we have reached the end of the list without duplicate)
return False
class Solution {
public boolean containsDuplicate(int[] nums) {
// Create Set
HashSet<Integer> hset = new HashSet<Integer>();
// Iterate through numbers
for (int idx = 0; idx < nums.length; idx ++){
// If number is already in set return True
if (hset.contains(nums[idx])){
return true;
}
// Store number in set
hset.add(nums[idx]);
}
// Return False (we have reached the end of the list without duplicate)
return false;
}
}
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of items in the array.
O(N)
because we need to traverse all numbers in the array.O(N)
because we may need to store all numbers in the array.