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
  1. A. 6
  2. B. 2
  3. C. 5
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic