Reviewed by Aditya Kumar Β· Last reviewed 2026-03-24
Hash set; only count from sequence start (no n-1 in set). Python: s=set(nums); best=0; for n in s: if n-1 not in s: c,x=1,n; while x+1 in s: c+=1; x+=1; best=max(best,c); return best. Key: only start when n-1 not in set (avoids recounting)....
Hash set; only count from sequence start (no n-1 in set). Python: s=set(nums); best=0; for n in s: if n-1 not in s: c,x=1,n; while x+1 in s: c+=1; x+=1; best=max(best,c); return best. Key: only start when n-1 not in set (avoids recounting). O(n) time, O(n) space.
Red Flag: O(n log n) sort solution. Pro-Move: 'Set + only start from sequence head = O(n); explain why we skip when n-1 in set.'
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.