Data Structures & AlgorithmsVery hardFlipkart/Uber OA · placement
For s = "cbbbccadbcdacd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by position) have at most 2 distinct characters?
- A. 6 and 38
- B. 7 and 38
- C. 6 and 24
- D. 5 and 38
Correct answer: A. 6 and 38
Shrink from the left whenever a third letter enters. The longest window is 6. For counting, every right end r contributes (r − left + 1) valid substrings; summing gives 38.
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
- Keys 81, 25, 26, 79, 49, 55, 59, 93 are inserted in this order into an empty table of size 11 with h(k) = k mod 11 and linear probing…
- Only the lower triangle (including the diagonal) of a 10 × 10 matrix is stored COLUMN by column in a 1-D array B starting at B[0] (indices…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [8, "pop", 8, 8, "pop"…
- Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed? python def revk(h, k): cnt…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[50, 81, 32, 23], [30, 61, 32, 23], [60, 51…
- A chained hash table stores n = 2,000 keys in m = 500 buckets with simple uniform hashing. What is the expected number of key comparisons…