← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026Remote

Summary

Meta SWE coding round, one question the whole time: longest consecutive sequence. Seemed straightforward until they pushed for the O(n) version and wanted me to justify the amortized complexity out loud.

Questions Asked (1)

Q1

Given an unsorted array of integers, find the length of the longest run of consecutive integers. Can you do it in O(n)?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I jumped straight to the sort-and-scan approach because it felt clean, and the interviewer just nodded and said 'can we do better.' Classic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a hash set to store all numbers for O(1) lookups, then iterate through the array and for each number that is the start of a consecutive sequence (i.e., num-1 not in set), count the length of the run. Keep track of the maximum length found. This achieves O(n) time because each number is visited at most twice.

Pro tip: Clarify that the array may contain duplicates and that the run must be consecutive integers (not necessarily consecutive in the array). Also, mention that the hash set approach uses O(n) extra space, which is a trade-off for achieving O(n) time.

1. Clarify the problem

Confirm that the array is unsorted, may contain duplicates, and that we need the length of the longest run of consecutive integers (e.g., 1,2,3,4). Ask if the array can be modified or if extra space is allowed.

2. Discuss brute force and sorting approaches

Mention that sorting would take O(n log n) and then a linear scan could find the longest run, but this doesn't meet the O(n) requirement. Brute force checking each number would be O(n^2) or worse.

3. Propose hash set solution

Explain that we can insert all numbers into a hash set for O(1) lookups. Then, for each number, check if it's the start of a sequence (i.e., num-1 not in set). If so, count upwards to find the length of the run.

4. Analyze time and space complexity

Show that each number is visited at most twice (once when checking if it's a start, and once when counting a run), so time is O(n). Space is O(n) for the hash set.

5. Handle edge cases and optimize

Consider empty array, single element, duplicates, and negative numbers. Mention that we can skip numbers that are not starts to avoid redundant work. Optionally, discuss if we can reduce space by using a different approach (e.g., if the range is known).

Key Points to Mention

  • Hash set for O(1) lookups
  • Only start counting from numbers that are the beginning of a sequence (num-1 not in set)
  • Each number is visited at most twice, ensuring O(n) time
  • Space complexity is O(n) due to the hash set
  • Duplicates are handled naturally by the set
  • Trade-off: O(n) space for O(n) time, compared to O(n log n) time and O(1) space with sorting

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.