Skip to content
Learn Netverks
Company prep Snap
Junior (1–3 years) Coding / DSA Medium

Find longest consecutive sequence in an unsorted array

Reported in Snap interview loops. Hash set based linear-time approach for sequence detection.

Location
Noida

Context for Snap candidates:

Return the length of the longest run of consecutive integers in any order.

Try answering aloud first

Cover trade-offs, structure, and a concrete example before revealing the baseline response.

Spoiler-free prep mode

How to frame this at Snap: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.

Insert all values into a hash set, then start counting only from numbers that have no predecessor (num - 1 absent). This prevents repeated work and ensures each sequence is expanded once.

For each valid start, advance while num + 1 exists and track maximum length. The average complexity is O(n), which is better than sorting-based O(n log n) for large input.

Comments (0)

Share how this question came up in your loop, or add tips for others preparing.

Log in to comment on this question.