ProgrammingHardTCS NQT · Accenture
The procedure below scans the string "abracadabra" from left to right and counts how many letters are repeats of an earlier letter (a letter already seen, counted each time it appears again). What is printed?
The procedure below scans the string "abracadabra" from left to right and counts how many letters are repeats of an earlier letter (a letter already seen, counted each time it appears again). What is printed?
seen = empty set; repeats = 0
for each letter ch
if ch in seen then repeats = repeats + 1 else add ch to seen
end for
Print repeats- A. 6
- B. 5
- C. 7
- D. 0
Correct answer: A. 6
abracadabra has 11 letters and 5 distinct ones, so 6 letters are repeats. Printing the distinct count gives 5, adding one to the difference of length and distinct count gives 7, and never adding to seen gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A string is a rotation of another when it can be obtained by moving some prefix to the end. The procedure below checks whether…
- The array [1, 2, 3, 4, 5, 6, 7] is shifted left by 2 positions by the procedure below: the element at index k is placed at index (k - 2)…
- The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including…
- The number of trailing zeros in the decimal form of 2^20 5^3 is found by the procedure below, which divides out factors of 10 one at a…
- The procedure below computes the integer square root of 200 by testing r = 0, 1, 2, ... and stopping at the largest r with (r + 1) (r + 1)…
- The procedure below finds the smallest 0-based index i at which the prefix sum of [4, -1, 3, -2, 5] reaches at least 7. It prints i. What…