Data Structures & AlgorithmsVery hardTCS NQT · Accenture
How many distinct non-empty subsequences (as strings) does the word aba have?
- A. 6
- B. 7
- C. 2
- D. 3
Correct answer: A. 6
Distinct strings: a, b, ab, ba, aa, aba = 6; the two index choices that both give 'a' collapse into one. 7 = 2³ − 1 counts index choices, 2 counts distinct letters and 3 is just the length.
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
- A print queue holds jobs with priorities [2, 1, 3, 2] (index 0 to 3). The front job prints only if no waiting job has a higher priority…
- A stack machine runs PUSH 3, PUSH 4, ADD, PUSH 5, MUL in order. The result is the value on top of the stack at the end. What is it?
- How many index pairs i < j in the array [1, 3, 1, 2, 3, 3] have a[i] = a[j]?
- What is the sum of the XOR of every non-empty contiguous subarray of [1, 2, 3]?
- How many contiguous subarrays of [2, 4, 8, 3, 6] have an LCM equal to their maximum element?
- How many ordered triples (i, j, k) of distinct indices in [2, 3, 5, 1, 4] satisfy a[k] = a[i] + a[j], counting each unordered pair {i, j}…