Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is printed?
Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is printed?
a = [17, 3, 21, 11, 19, 7, 1, 23, 5, 4, 20]
s, best, starts = set(a), 0, 0
for x in s:
if x - 1 not in s:
starts += 1; y = x
while y + 1 in s: y += 1
best = max(best, y - x + 1)
print(best, starts)- A. 3 11
- B. 5 7
- C. 3 7
- D. 2 7
Correct answer: C. 3 7
Only sequence starts (x with x − 1 absent) launch a scan, which keeps the whole pass O(n). Output (longest, number of runs): 3 7.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [-1, 0, 2, 2, 2] n =…
- What is the maximum product of a non-empty contiguous subarray of [−2, 4, 3, −1, −2, −3, −2, 0]? (Track both the maximum and the minimum…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…
- What does this loop-counting code print? (Python) python n, c = 37, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [14, 8, 20, 57, 38, 4…