ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode (duplicate detection using a set)?
What will be the output of the following pseudocode (duplicate detection using a set)?
Function hasDuplicate(Array arr)
Set seen = empty set
for (each x in arr)
if (x in seen)
return true
end if
add(seen, x)
end for
return false
End Function
Print hasDuplicate({1, 2, 3, 4, 2}), hasDuplicate({1, 2, 3})- A. true false
- B. false true
- C. false false
- D. true true
Correct answer: A. true false
The first array has 2 appearing twice, so the function finds it already in seen on the second pass and returns true. The second array has no repeats, so it returns false after scanning fully.
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 (maximum sum of any window of size k)? (Arrays are indexed from 0.) Integer arr[6] =…
- What will be the output of the following pseudocode (counting inversions, brute force)? (Arrays are indexed from 0.) Integer arr[5] = {2…
- What will be the output of the following pseudocode (counting pairs summing to a target, brute force)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (counting pattern occurrences, allowing overlaps)? Function countOcc(String text…
- What will be the output of the following pseudocode (manually parsing a signed integer string)? Function strToInt(String s) Integer sign =…
- What will be the output of the following pseudocode (counting trailing zeros in n factorial, via counting factors of 5)? (All variables…