← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta SWE coding round, just the one algorithmic problem about consecutive sequences. Pretty standard for them but the O(n) constraint is where people trip up.

Questions Asked (1)

Q1

Given an unsorted array of integers, return the length of the longest consecutive elements sequence. Your solution must run in O(n) time.

Algorithms & Data Structures
Author's notes

The naive sort-based solution is O(n log n) and I kept second-guessing whether they'd accept it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a hash set to store all elements for O(1) lookups. For each number, check if it's the start of a sequence (i.e., num-1 not in set), then count consecutive numbers. This ensures each element is visited at most twice, achieving O(n) time.

Pro tip: Clarify that the O(n) time complexity relies on hash set operations being O(1) on average, and mention that the space complexity is O(n) due to the set. Also, discuss edge cases like empty array or duplicates.

1. Understand the problem

Restate the problem: find the length of the longest consecutive sequence in an unsorted array, with O(n) time. Confirm that elements can be in any order and duplicates may exist.

2. Choose data structure

Use a hash set to store all unique elements, enabling O(1) average-time membership checks. This is key to achieving linear time.

3. Identify sequence starts

For each number in the set, check if it is the start of a sequence by verifying that num-1 is not in the set. Only start counting from these numbers to avoid redundant work.

4. Count consecutive elements

From each start, incrementally check for num+1, num+2, etc., in the set, counting the length. Update the maximum length found.

5. Analyze complexity

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

Key Points to Mention

  • Hash set for O(1) lookups
  • Only start counting from sequence beginnings (num-1 not in set)
  • Each element visited at most twice, ensuring O(n) time
  • Space complexity O(n) due to the set
  • Handling duplicates by using a set
  • Edge cases: empty array, single element, all duplicates

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