I jumped straight to the sort-and-scan approach because it felt clean, and the interviewer just nodded and said 'can we do better.' Classic.
Use a hash set to store all numbers for O(1) lookups, then iterate through the array and for each number that is the start of a consecutive sequence (i.e., num-1 not in set), count the length of the run. Keep track of the maximum length found. This achieves O(n) time because each number is visited at most twice.
Pro tip: Clarify that the array may contain duplicates and that the run must be consecutive integers (not necessarily consecutive in the array). Also, mention that the hash set approach uses O(n) extra space, which is a trade-off for achieving O(n) time.
Confirm that the array is unsorted, may contain duplicates, and that we need the length of the longest run of consecutive integers (e.g., 1,2,3,4). Ask if the array can be modified or if extra space is allowed.
Mention that sorting would take O(n log n) and then a linear scan could find the longest run, but this doesn't meet the O(n) requirement. Brute force checking each number would be O(n^2) or worse.
Explain that we can insert all numbers into a hash set for O(1) lookups. Then, for each number, check if it's the start of a sequence (i.e., num-1 not in set). If so, count upwards to find the length of the run.
Show that each number is visited at most twice (once when checking if it's a start, and once when counting a run), so time is O(n). Space is O(n) for the hash set.
Consider empty array, single element, duplicates, and negative numbers. Mention that we can skip numbers that are not starts to avoid redundant work. Optionally, discuss if we can reduce space by using a different approach (e.g., if the range is known).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.