Data Structures & AlgorithmsVery hardTCS NQT Digital · Product Company SDE

Problem: Among all distinct permutations of the letters A, B, C, D, arranged in dictionary order starting from rank 1, what is the rank of "CADB"?

  1. A. 13
  2. B. 16
  3. C. 11
  4. D. 14

Correct answer: D. 14

C has 2 smaller remaining letters (2 · 3! = 12), A has 0, D has 1 smaller remaining letter (1 · 1! = 1), so 13 permutations precede CADB and its rank is 14. Forgetting the + 1 gives 13, counting smaller letters overall instead of among the remaining (3 for D) gives 16, and ranking from the end of the list (24 − 14 + 1) gives 11.

More Coding Interview Problems questions

Exams that ask this topic