Data Structures & AlgorithmsHardTCS Digital · Infosys SP
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 = [14, 9, 16, 18, 12, 10, 17, 23, 3, 11, 21]
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. 4 11
- B. 4 6
- C. 5 6
- D. 4 5
Correct answer: B. 4 6
Only sequence starts (x with x − 1 absent) launch a scan, which keeps the whole pass O(n). Output (longest, number of runs): 4 6.
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
- What does this loop-counting code print? (Python) python n, c = 20, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 5, 1, 2, 3, 7, 4]. Treating i → a[i] as a linked…
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…
- Which statement about the KMP failure (lps) array of a pattern of length m is correct?
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [18…