Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Given the words ba, b and a, concatenate all three in some order. What is the lexicographically smallest resulting string?

  1. A. abab
  2. B. {C+1}
  3. C. {C+2}
  4. D. {C+4}

Correct answer: A. abab

Comparing words by a+b versus b+a gives the right order; here the best concatenation is abab. Sorting plainly by the word itself gives a different order.

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

Exams that ask this topic