Data Structures & AlgorithmsHardTCS NQT · Infosys

What is the lexicographically smallest string obtainable from cbad by swapping exactly one pair of characters?

  1. A. cbad
  2. B. bcad
  3. C. abcd
  4. D. acbd

Correct answer: C. abcd

Swapping positions 0 and 2 (c and a) gives abcd, the smallest string reachable by one swap, since it puts the smallest letter first. Check every pair of positions.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic