ProgrammingHardTCS NQT · Accenture

The procedure below scans the word "leetcode" from the left and prints the index of the first letter that occurs exactly once in the word. What is printed?

The procedure below scans the word "leetcode" from the left and prints the index of the first letter that occurs exactly once in the word. What is printed?
count each letter
for i = 0 to n - 1
    if count of s[i] == 1 then print i; stop
end for
  1. A. 0
  2. B. 6
  3. C. 1
  4. D. 5

Correct answer: A. 0

The first letter l occurs only once, so the answer is index 0. Scanning from the end finds d at index 6, testing for a count of 3 finds the first e at index 1, and counting the unique letters gives 5.

More Pseudocode and Code Tracing questions

Exams that ask this topic