ProgrammingHardTCS NQT · Accenture
The string "bananana" is searched for "ana" with overlapping occurrences allowed. The procedure below counts the start positions of matches. What is printed?
The string "bananana" is searched for "ana" with overlapping occurrences allowed. The procedure below counts the start positions of matches. What is printed?
count = 0
for i = 0 to len(s) - 3
if s[i..i+2] == "ana" then count = count + 1
end for
Print count- A. 2
- B. 4
- C. 5
- D. 3
Correct answer: D. 3
Matches begin at positions 1, 3 and 5 of bananana, so the count is 3. Skipping past each match finds 2, counting the letter a gives 4, and also accepting nan finds 5.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Strings are compressed in place by writing each run of equal characters as the character followed by the run length (a count of 1 is…
- Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every…
- The word "mississippi" is examined by the procedure below, which counts how many different letters it contains. What is printed? seen =…
- A palindrome check on "racecar" uses two pointers from both ends, comparing one pair per step and stopping early on a mismatch. The…
- The string "abcabcbb" is scanned with a sliding window that keeps the longest stretch without a repeated character. The procedure below…
- Scanning "abcab dab" as a string of letters, the procedure below counts the adjacent pairs that read "ab" (both letters next to each…