ProgrammingHardInfosys · Cognizant
The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using a set. What is printed?
The number of distinct substrings (non-empty, counted once per distinct text) of the string "aab" is computed by the procedure below using a set. What is printed?
seen = empty set for each start i, each end j > i: add s[i..j-1] to seen Print size of seen
- A. 6
- B. 2
- C. 5
- D. 3
Correct answer: C. 5
The distinct substrings of aab are a, aa, aab, ab and b, which is 5. Counting all 6 substrings counts the repeated a, the distinct letters give 2, and the distinct prefixes give 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Two positions in the word "hello" are chosen (the letters at the two positions form a pair, and positions are distinct). The procedure…
- The procedure below counts the cyclic rotations of "abab" that are equal to "abab" itself, checking the rotations obtained by moving k…
- Coin denominations are 1, 4 and 6. The greedy method below pays amount 8 by always taking the largest coin that still fits (coins are…
- Ropes of lengths 1, 2, 3, 4 and 5 are merged two at a time with cost equal to the sum of the two lengths. The greedy merges the two…
- A truck carries at most 10 boxes. The box types are given as (units per box, number of boxes): (5, 3), (4, 2), (3, 4), (2, 5). The greedy…
- The greedy below repeatedly increases the smallest element of the array [3, 1, 2] by 1, three times in total. What is the minimum value of…