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 = [100,4,200,1,3,2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.
Input: nums = [0,3,7,2,5,8,4,6,0,1]
Output: 9
EDGE CASE
Input: []
Output: 0
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 and find subsequence length
1. Sort the array
2. Keep track of current length count (current) and longest length count (longest). Both starting at 1.
3. Loop through the sorted array
4. Skip duplicates
5. If current number is one more than previous number, increment current streak
6. Else set longest streak and reset current streak
7. Return the longest streak
General Idea: Approach #2 Use a hash set to find the longest length
1. Keep track of longestStreak starting at 0.
2. Create a hashset
3. Loop through the number set
4. If current number has no previous number, then start counting from this number
5. While the set contains currentNum + 1, increment currentNum and currentStreak
6. After each while loop, check and set longestStreak
7. Return longestStreak
โ ๏ธ Common Mistakes
Implement the code to solve the algorithm.
Approach #1 Sort and find subsequence length
class Solution:
def longestConsecutive(self, nums):
if not nums:
return 0
# Sort the array
nums.sort()
# Keep track of current length count (current) and longest length count (longest). Both starting at 1.
longest_streak = 1
current_streak = 1
# Loop through the sorted array
for i in range(1, len(nums)):
# skip duplicates
if nums[i] == nums[i-1]:
continue
# If current number is one more than previous number add to current streak
if nums[i] == nums[i-1]+1:
current_streak += 1
# Else set longest streak and reset current streak
else:
longest_streak = max(longest_streak, current_streak)
current_streak = 1
# Return the longest streak
return max(longest_streak, current_streak)
Approach #2 Use a hash set to find the longest length
class Solution:
def longestConsecutive(self, nums):
# Keep track of longestStreak starting at 0.
longest_streak = 0
# Create a hashset
num_set = set(nums)
# Loop through the number set
for num in num_set:
# If current number has no previous number, then start counting from this number
if num - 1 not in num_set:
current_num = num
current_streak = 1
# While the set contains currentNum + 1, increment currentNum and currentStreak
while current_num + 1 in num_set:
current_num += 1
current_streak += 1
# After each while loop, check and set longestStreak
longest_streak = max(longest_streak, current_streak)
# Return longestStreak
return longest_streak
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 at the first glance, the while loop inside the for loop may seem like the time complexity is O(n * n). However, the while loop is only reached when the currentNum is the first of a subsequence, the while loop can only run for n iterations throughout the entire runtime of the function. So the runtime is actually O(n+n) = O(n).O(N)
because we store the numbers in a hashset.