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")
  1. A. true true
  2. B. false false
  3. C. true false
  4. 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'.

More Pseudocode and Code Tracing questions

Exams that ask this topic