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- A. 0
- B. 6
- C. 1
- 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.
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 integers n with 1 <= n <= 60 that are not divisible by 2, 3 or 5 (coprime to 30). What is printed? count =…
- The procedure below counts the pairs i < j in the array [3, 1, 4, 1, 5] whose sum is even. What is printed? count = 0 for i = 0 to n - 2…
- The procedure below collects x mod 5 for every x from 1 to 20 into a set and prints the size of the set. What is printed? seen = empty set…
- Array a = [1, 1, 1, 1] (0-based). From index i you may jump forward by at most a[i] positions. The greedy below counts the minimum number…
- The procedure below sums the decimal digits of 2^10 = 1024 once and prints the result. What is printed? x = 2^10; total = 0 while x > 0…
- The procedure below counts the inversions of the array [2, 4, 1, 3] (pairs i < j with a[i] > a[j]) by checking all pairs. What is printed?…