Only values without a predecessor start a run; then advance one value at a time.
Problem trace
Start a walk only where the consecutive run has no predecessor
Read it this way:For set (1,2,3,8), start at 1 and 8. Starting again at 2 or 3 repeats work inside the already-covered run. Does rejecting two as a new run start remove two from the existing one-to-three run?
Sources and provenance
Full interview answer 22 min read
Find the longest run of consecutive integer values present anywhere in an unordered input list.
Problem
Input. An unsorted integer list nums, possibly containing negatives and duplicates.
Output. Return the greatest number of distinct values in any run increasing by exactly one each time.
Rules. Values need not be adjacent or appear in increasing input order.
Repeated copies of a value do not extend a run. Return only the length, not the values or indices.
Do not change the input. Empty input returns zero.
Example.[100, 4, 200, 1, 3, 2] returns 4 for the run 1, 2, 3, 4.
For [1, 1, 2], return 2, because the repeated 1 adds no new consecutive value.
Picture: Expand Only a Run’s First Value
Problem trace
Separate full set: walk runs only from predecessor-free starts
Input and goalFind the length of the longest run of consecutive values in an unsorted array.
Build the membership setDeduplicate nums into {1,2,3,4,100,200}; best starts at 0. Sorted display does not change set membership or the answer.
next candidate 11021324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryset{1,2,3,4,100,200}
scalar state and membership querybest0
key best
0
next candidate 1
0
example
nums = [100, 4, 200, 1, 3, 2]; set displayed in sorted order
Recognize start 1For candidate 1, predecessor 0 is absent, so initialize end=1 and enter the while loop.
start=1end=11021324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchstart a run
scalar state and membership querybest0query0 absent
key query
not shown→0 absent
end
not shown→0
branch
not shown→start a run
Advance end to 2end+1=2 is in the set, so the while loop moves end from 1 to 2.
start=110end: 1 -> 221324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength2 - 1 + 1 = 2
scalar state and membership querybest0query2 present
key query
0 absent→2 present
end
0→1
run length
not shown→2 - 1 + 1 = 2
Advance end to 3end+1=3 is present, so end moves from 2 to 3.
start=11021end: 2 -> 3324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength3 - 1 + 1 = 3
scalar state and membership querybest0query3 present
key query
2 present→3 present
end
1→2
run length
2 - 1 + 1 = 2→3 - 1 + 1 = 3
Advance end to 4end+1=4 is present, so end moves from 3 to 4.
start=1102132end: 3 -> 44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength4 - 1 + 1 = 4
scalar state and membership querybest0query4 present
key query
3 present→4 present
end
2→3
run length
3 - 1 + 1 = 3→4 - 1 + 1 = 4
Stop the run and update bestend+1=5 is absent. The completed length is 4-1+1=4, so best changes from 0 to 4.
start=1102132end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(0, 4 - 1 + 1) = 4
scalar state and membership querybest4query5 absent
key best
0→4
key query
4 present→5 absent
arithmetic
not shown→max(0, 4 - 1 + 1) = 4
Skip candidate 2For candidate 2, predecessor 1 is present. Continue immediately so the run [1,2,3,4] is not counted again.
10skip start=22132completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query1 present
key query
5 absent→1 present
skip start
0→1
branch
not shown→continue
Skip candidate 3For candidate 3, predecessor 2 is present, so continue.
1021skip start=332completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query2 present
key query
1 present→2 present
skip start
1→2
Skip candidate 4For candidate 4, predecessor 3 is present, so continue.
102132skip start=4completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query3 present
key query
2 present→3 present
skip start
2→3
Measure singleton 100For 100, predecessor 99 is absent, but successor 101 is also absent. Its length is 100-100+1=1, so best stays 4.
10213243start=100end=10010042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(4, 100 - 100 + 1) = 4
scalar state and membership querybest4queries99 absent; 101 absent
key queries
not shown→99 absent; 101 absent
key query
3 present→not shown
start
3→4
Measure singleton 200For 200, predecessor 199 and successor 201 are absent. Its length is 1, so best remains 4.
102132431004start=200end=2002005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(4, 200 - 200 + 1) = 4
scalar state and membership querybest4queries199 absent; 201 absent
key queries
99 absent; 101 absent→199 absent; 201 absent
start
4→5
end
4→5
Return the longest lengthAll set values are either a measured run start or were skipped because their predecessor exists. Return best=4.
best run start102132best run end4310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querylongestRun[1,2,3,4]result4
scalar state and membership querybest4
key queries
199 absent; 201 absent→not shown
best run start
5→0
best run end
5→3
Recognize it
The input is unsorted and asks for consecutive integer values, not consecutive positions, while near-linear time rules out sorting as the intended mechanism.
Keep true
A run is expanded only from its unique smallest value, identified by missing predecessor; best is the maximum completed run length seen so far.
Reuse it
Before expanding a component, find a unique boundary that only one member can satisfy; this avoids duplicate work in runs, intervals, and component scans.
Deduplicate nums into {1,2,3,4,100,200}; best starts at 0. Sorted display does not change set membership or the answer. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why does value 2 not start a second walk? Why does that matter for total work?
Reference drawing
Problem trace
Start a walk only where the consecutive run has no predecessor
Execution trace
Input and goalFind the length of the longest run of consecutive values in an unsorted array.
Build the membership setDeduplicate nums into {1,2,3,4,100,200}; best starts at 0. Sorted display does not change set membership or the answer.
next candidate 11021324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryset{1,2,3,4,100,200}
scalar state and membership querybest0
key best
0
next candidate 1
0
example
nums = [100, 4, 200, 1, 3, 2]; set displayed in sorted order
Recognize start 1For candidate 1, predecessor 0 is absent, so initialize end=1 and enter the while loop.
start=1end=11021324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchstart a run
scalar state and membership querybest0query0 absent
key query
not shown→0 absent
end
not shown→0
branch
not shown→start a run
Advance end to 2end+1=2 is in the set, so the while loop moves end from 1 to 2.
start=110end: 1 -> 221324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength2 - 1 + 1 = 2
scalar state and membership querybest0query2 present
key query
0 absent→2 present
end
0→1
run length
not shown→2 - 1 + 1 = 2
Advance end to 3end+1=3 is present, so end moves from 2 to 3.
start=11021end: 2 -> 3324310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength3 - 1 + 1 = 3
scalar state and membership querybest0query3 present
key query
2 present→3 present
end
1→2
run length
2 - 1 + 1 = 2→3 - 1 + 1 = 3
Advance end to 4end+1=4 is present, so end moves from 3 to 4.
start=1102132end: 3 -> 44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryrunLength4 - 1 + 1 = 4
scalar state and membership querybest0query4 present
key query
3 present→4 present
end
2→3
run length
3 - 1 + 1 = 3→4 - 1 + 1 = 4
Stop the run and update bestend+1=5 is absent. The completed length is 4-1+1=4, so best changes from 0 to 4.
start=1102132end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(0, 4 - 1 + 1) = 4
scalar state and membership querybest4query5 absent
key best
0→4
key query
4 present→5 absent
arithmetic
not shown→max(0, 4 - 1 + 1) = 4
Skip candidate 2For candidate 2, predecessor 1 is present. Continue immediately so the run [1,2,3,4] is not counted again.
10skip start=22132completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query1 present
key query
5 absent→1 present
skip start
0→1
branch
not shown→continue
Skip candidate 3For candidate 3, predecessor 2 is present, so continue.
1021skip start=332completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query2 present
key query
1 present→2 present
skip start
1→2
Skip candidate 4For candidate 4, predecessor 3 is present, so continue.
102132skip start=4completed end=44310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querybranchcontinue
scalar state and membership querybest4query3 present
key query
2 present→3 present
skip start
2→3
Measure singleton 100For 100, predecessor 99 is absent, but successor 101 is also absent. Its length is 100-100+1=1, so best stays 4.
10213243start=100end=10010042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(4, 100 - 100 + 1) = 4
scalar state and membership querybest4queries99 absent; 101 absent
key queries
not shown→99 absent; 101 absent
key query
3 present→not shown
start
3→4
Measure singleton 200For 200, predecessor 199 and successor 201 are absent. Its length is 1, so best remains 4.
102132431004start=200end=2002005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership queryarithmeticmax(4, 200 - 200 + 1) = 4
scalar state and membership querybest4queries199 absent; 201 absent
key queries
99 absent; 101 absent→199 absent; 201 absent
start
4→5
end
4→5
Return the longest lengthAll set values are either a measured run start or were skipped because their predecessor exists. Return best=4.
best run start102132best run end4310042005
examplenums = [100, 4, 200, 1, 3, 2]; set displayed in sorted ordermapLabelscalar state and membership querylongestRun[1,2,3,4]result4
scalar state and membership querybest4
key queries
199 absent; 201 absent→not shown
best run start
5→0
best run end
5→3
Recognize it
The input is unsorted and asks for consecutive integer values, not consecutive positions, while near-linear time rules out sorting as the intended mechanism.
Keep true
A run is expanded only from its unique smallest value, identified by missing predecessor; best is the maximum completed run length seen so far.
Reuse it
Before expanding a component, find a unique boundary that only one member can satisfy; this avoids duplicate work in runs, intervals, and component scans.
Deduplicate nums into {1,2,3,4,100,200}; best starts at 0. Sorted display does not change set membership or the answer. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why does value 2 not start a second walk? Why does that matter for total work?
Read it this way:For set (1,2,3,8), start at 1 and 8. Starting again at 2 or 3 repeats work inside the already-covered run. Does rejecting two as a new run start remove two from the existing one-to-three run?
The sorted set display is not input order or execution order. Small cell indices locate displayed values only.
Candidate
Predecessor
Action
1
0 absent
Advance through 2, 3, 4; missing 5 ends length 4
2, 3, 4
Present
Skip these interior starts
100
99 absent
Singleton; best stays 4
200
199 absent
Singleton; best stays 4
Each distinct value gets one predecessor test. Each belongs to one expanded run; successful successor steps total u−c.
Here u counts distinct values and c counts maximal runs. Each run also ends with one failed successor query.
Thus all successor queries total u, not u2, regardless of set iteration order.
Code
def longest_consecutive(nums: list[int]) -> int: values = set(nums) best = 0 for start in values: if start - 1 in values: continue end = start while end + 1 in values: end += 1 best = max(best, end - start + 1) return best
Expected time is O(n), including set construction; space is O(u), with u≤n.
This assumes expected constant-time hashing and ordinary integer arithmetic.
Change 1: Return a Deterministic Witness
New task. Return run values, choosing the smallest start on ties.
Picture and change. Save length and start; materialize only the winner.
Code.
def longest_run_values(nums): values = set(nums) best_start, best_length = None, 0 for start in values: if start - 1 in values: continue end = start while end + 1 in values: end += 1 length = end - start + 1 if length > best_length or (length == best_length and start < best_start): best_start, best_length = start, length return [] if best_start is None else list(range(best_start, best_start + best_length))
Use when. A reproducible witness matters. Its output storage is additional.
Change 2: A Fixed Positive Integer Step
New task. Consecutive members differ by step, not necessarily 1.
Picture and change. Test start-step, advance end+step, and measure length in steps.
Code.
def longest_fixed_step(nums, step): if step <= 0: raise ValueError("positive step required") values = set(nums) best = 0 for start in values: if start - step in values: continue end = start while end + step in values: end += step best = max(best, (end - start) // step + 1) return best
Check.[1, 3, 5, 2, 4], step 2, gives 3; [-3, -1, 1] also gives 3.
Step zero raises an error instead of invalidating progress.
Use when. A fixed successor spacing still partitions values into disjoint runs.
Failure: Starting From Every Member
Expanding suffixes from every value of a length-u run costs u+(u−1)+⋯+1 work, which is quadratic.
Transfer Check
Why do the nested loops not make the reference quadratic?
Reasoned Answer
Only the unique start expands each disjoint run. Charge each successful advance to a distinct value, not every outer iteration.
What Information Does the Set Discard?
It forgets input order and multiplicity, neither of which changes this length-only answer.
An adjacent-position requirement would be different: [1, 3, 2] contains value run 1, 2, 3, not an adjacent increasing run.
That task needs positional information the set discarded.
The outer loop must use distinct values too. Repeating a run’s head from the original duplicated input can rescan the same run.
The predecessor guard alone does not prevent those repeated-head scans.
The witness tie rule is needed only because the output now selects particular values.
The reference’s maximum length does not depend on iteration order.
Fixed positive spacing preserves one predecessor and successor per integer, but a general graph can branch or cycle.
Do not transfer this start-only proof to arbitrary connected components without traversal and visited-state reasoning.