← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
Design a data structure that supports adding integers and querying the length of the longest consecutive sequence formed by the integers added so far. After each insertion you must be able to report the current longest streak of consecutive numbers. For example, after adding 1, 2, 4, 3 the longest consecutive sequence is 1-2-3-4 so the answer is 4. Implement the class ConsecutiveTracker with:
ConsecutiveTracker() initializes the tracker.int add(int num) adds the integer num to the data structure and returns the length of the longest consecutive sequence that exists after this insertion. Duplicates are ignored; adding a number that is already present leaves the state unchanged and simply returns the current longest streak.Your implementation must handle each add call efficiently; the evaluator expects amortized O(1) time per insertion and O(1) time to retrieve the running maximum.