ProgrammingHardAccenture · Cognizant pattern
What will be the output of the following pseudocode (checking whether all characters in a string are unique)?
What will be the output of the following pseudocode (checking whether all characters in a string are unique)?
Function hasUniqueChars(String s)
Set seen = empty set
for (each ch in s)
if (ch in seen)
return false
end if
add(seen, ch)
end for
return true
End Function
Print hasUniqueChars("abcdef"), hasUniqueChars("hello")- A. true true
- B. false false
- C. true false
- D. false true
Correct answer: C. true false
'abcdef' has 6 distinct letters, no repeats: true. 'hello' repeats the letter 'l', so the function returns false as soon as it hits the second 'l'.
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 (Hamming distance: number of differing bits between two integers)? (^ is bitwise XOR…
- What will be the output of the following pseudocode (finding the minimum in a rotated sorted array via binary search)? (Arrays are indexed…
- What will be the output of the following pseudocode (moving all zeros to the end while preserving the relative order of non-zero elements…
- What will be the output of the following pseudocode (length of the longest substring without repeating characters, sliding window)?…
- What will be the output of the following pseudocode (number of bits needed to represent n in binary)? Function bitsNeeded(Integer n)…
- What will be the output of the following pseudocode (counting trailing zero bits in the binary representation of n)? (All variables are…