ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting distinct substrings of length 2)?
What will be the output of the following pseudocode (counting distinct substrings of length 2)?
String s = "abab"
Set subs = empty set
for (each i from 0 to length(s) - 2)
add(subs, substring(s, i, i + 2))
end for
Print size(subs)- A. 3
- B. 2
- C. 4
- D. 1
Correct answer: B. 2
The length-2 substrings scanned are 'ab' (i=0), 'ba' (i=1), 'ab' (i=2); the set keeps only distinct ones, {'ab', 'ba'}, giving size 2.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 20, steps = 0…
- What will be the output of the following pseudocode (counting all subsets of a set of size n)? Function countSubsets(Integer n) if (n ==…
- What will be the output of the following pseudocode (counting set bits using Brian Kernighan's trick, for each value 0 to 7)? (& is…
- What will be the output of the following pseudocode (longest common prefix of a list of strings)? Function lcp(List strs) String prefix =…
- What will be the output of the following pseudocode (finding the kth smallest element by sorting)? (Arrays are indexed from 0.) Integer…
- What will be the output of the following pseudocode (number of ways to climb n stairs, taking 1 or 2 steps at a time)? Function…