ProgrammingModerateAccenture · Cognizant GenC Next

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Function isSub(String a, String b)        // is b a subsequence of a?
    Integer j = 0
    for (each i from 0 to length(a) - 1)
        if (j < length(b) AND a[i] == b[j]) j = j + 1
    end for
    return j == length(b)
End Function
Print isSub("ABCDE", "ACE"), isSub("ABCDE", "AEC"), isSub("AXBXC", "ABC")
  1. A. true false true
  2. B. true true true
  3. C. true false false
  4. D. false false true

Correct answer: A. true false true

A greedy single pass matches b's letters in order. ACE appears in order in ABCDE (true); AEC does not, since C comes before E (false); A, B, C appear in order in AXBXC with gaps (true). Output true false true.

More Pseudocode and Code Tracing questions

Exams that ask this topic