Longest Run of Consecutive User IDs in an Unsorted Signup Batch

Last updated: November 29, 2026

Quick Overview

Given an unsorted list of integer user IDs from a signup batch, return the length of the longest run of consecutive integers present, in O(n) time.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given an unsorted list of integer user IDs from a signup batch, return the length of the longest run of consecutive integers present, in O(n) time.

Asked in Meta onsites to see if you can beat sorting using a hash set. The interviewer wants the insight that you only start counting from the beginning of a run, which is what keeps the total work linear.

What the Interviewer Expects
  • Mentions the O(n log n) sort-and-scan approach first
  • Builds a set and only expands from numbers whose predecessor is absent
  • Explains why each number is visited a constant number of times overall
  • Handles duplicates and negative IDs
  • Gives O(n) average time and O(n) space
Key Topics to Cover
hash set
sequence detection
linear time
union find
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return the run itself (start and end), not just its length.
  • IDs arrive as a stream; maintain the longest run after each insertion.
  • How would you compute this when the IDs are spread across many machines?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: ids, a list of integers with possible duplicates and negatives, length up to 10^6. Output: the maximum length L such that some x, x+1, ..., x+L-1 are all present. Empty input gives 0....

High-Level Design

Put everything in a set. For each value x in the set, it starts a run only if x - 1 is missing. From each start, walk upward while x + k is present and record the length. Every element is touche...


Submit Your Answer
Markdown supported