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: [1, 2, 3, 2, 1, 2, 3, 2, 1], k = 2
Output: 3
Input: [1, 2, 3], k = 3
Output: null
EDGE CASE
Input: [1, 2, 2, 1, 4, 5], k = 2
Output: 1 or 2
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: Approach #1 Sort the array and find the number
1. Sort the array
2. Keep a counter of number of times seen
3. Loop through the sorted array
4. If current number is equal to previous number increase n
5. else check
- if n equals k then return number
- else reset seen to 0 and continue
6. If seen never equals k then return None
General Idea: Approach #2 Use a hash map to find number.
1. Create a hashmap
2. Loop through the array
3. Count number times seen using hashmap
4. Loop through hashmap to check if seen is equal to k
5. Return number if seen is equal to k
6. If seen never equals k then return None
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> int:
# Sort the array
nums.sort()
# Keep a counter of number of times seen
seen = 0
# Loop through the sorted array
for i in range(len(nums)):
# If current number is equal to previous number increase n
if nums[i] == nums[i - 1]:
seen += 1
else:
# if seen equals k then return the number
if seen == k:
return nums[i - 1]
# else reset seen to 0 and continue
else:
seen = 0
# If seen never equals k then return None
return None
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> int:
# Create a hashmap
hashmap = dict()
# Loop through the array
for i in range(len(nums)):
# Count number times seen using hashmap
if nums[i] in hashmap:
hashmap[nums[i]] += 1
else:
hashmap[nums[i]] = 1
# Loop through hashmap to check if seen is equal to k
for num, seen in hashmap.items:
# Return number if found
if seen == k:
return num
# If seen never equals k then return None
return None
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 array.
O(N)
because we loop through the array once.O(N)
because if each number in array is seen once, then each number in array is stored in hashmap.