ProgrammingHardTCS NQT · Accenture
The number of non-empty substrings of "abcd" (substrings are contiguous pieces, counted by position) is computed by the procedure below. What is printed?
The number of non-empty substrings of "abcd" (substrings are contiguous pieces, counted by position) is computed by the procedure below. What is printed?
count = 0
for i = 0 to n - 1
for j = i to n - 1: count = count + 1
end for
Print count- A. 6
- B. 16
- C. 8
- D. 10
Correct answer: D. 10
Substrings of length 1, 2, 3 and 4 number 4, 3, 2 and 1, giving 10 in total. Requiring j greater than i gives 6, the number of subsets is 16, and adding the distinct count to the length gives 8.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The procedure below counts the uppercase letters in the sentence "Hello World From AI". What is printed? count = 0 for each character c if…
- Strings are compared by dictionary order. The procedure below counts how many of ["apple", "mango", "kiwi", "banana", "mangoes"] are…
- A message of digits is decoded by mapping 1 to A, 2 to B, ..., 26 to Z, where a digit string may be split into one- or two-digit groups…
- A polynomial rolling hash of the string "abc" is computed as h = (h 31 + value) mod 1000, starting from h = 0, where value is the position…
- The string "bca" may have exactly one swap of two characters made. The procedure below finds the lexicographically smallest string…
- The phrase "the cat and the hat" is searched for the whole word "the" (counted as a separate word). The procedure below counts the…