ProgrammingHardAccenture · Cognizant
Longest common prefix of the words "flower", "flow" and "flight" is found by comparing characters column by column until a mismatch. The procedure below prints the length of that prefix. What is printed?
Longest common prefix of the words "flower", "flow" and "flight" is found by comparing characters column by column until a mismatch. The procedure below prints the length of that prefix. What is printed?
prefix = first word
for each other word w
shrink prefix until w starts with prefix
end for
Print length of prefix- A. 4
- B. 2
- C. 3
- D. 0
Correct answer: B. 2
The shared prefix is fl, of length 2, because flow and flight both start with fl but flower's third letter o does not match flight's i. Reading the second word gives 4, counting the words gives 3, and dropping the first letter instead of the last gives 0.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- The words of "the sky is blue" are reversed in order (the words themselves are kept). The procedure below does this and prints the result…
- The string "bananana" is searched for "ana" with overlapping occurrences allowed. The procedure below counts the start positions of…
- 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…