Data Structures & AlgorithmsModerateCAT · TCS NQT
Problem: Five people (including Alice and Bob) stand in a row. In how many arrangements do Alice and Bob stand next to each other?
- A. 48
- B. 24
- C. 120
- D. 8
Correct answer: A. 48
Glue Alice and Bob into one block: 4! arrangements × 2 internal orders = 48. Forgetting the internal order gives 24, 120 is every arrangement, and 4 adjacent slots × 2 orders = 8 forgets to arrange the other three.
More Coding Interview Problems questions
- Problem: The array [3, 1, 2] undergoes one pass of bubble sort (compare each adjacent pair from left to right and swap if out of order)…
- Problem: In the array [2, 9, 4, 7, 1, 8], find the 4th smallest element using the quickselect idea (its final answer is the value that…
- Problem: In the sorted array [1, 2, 4, 7, 8, 11, 13], how many pairs (i, j) with i < j have a[j] − a[i] ≤ 2?
- Problem: Piles of bananas are [3, 6, 7, 11] and you must finish all of them within 8 hours. Each hour you eat up to k bananas from one…
- Problem: Intervals [1,4], [3,8], [6,9], [10,12], [2,5] are given. How many of them contain the point 5 (endpoints included)?
- Problem: Compute (1³ + 2³ + … + 100³) modulo 1000.