Data Structures & AlgorithmsVery hardICPC regional · CAT · MBA placement
Problem: For the word-count test: how many distinct strings can be formed by rearranging "AABBCCD" so that no two identical letters are adjacent?
- A. 630
- B. 246
- C. 90
- D. 270
Correct answer: B. 246
Inclusion–exclusion over the pairs AA, BB, CC: 630 − 3·180 + 3·60 − 24 = 246, matching a brute force. Ignoring the restriction gives 630, stopping after subtracting single glued pairs gives 90, and forgetting the final −24 term gives 270.
More Coding Interview Problems questions
- Problem: A game uses 5 players in a circle numbered 1 to 5. The player with the ball passes it 3 positions clockwise each turn (the ball…
- What does this program print? python a = [4, 1, 3, 2] best = 0 for i in range(len(a)): for j in range(i + 1, len(a) + 1): best = max(best…
- What does this program print? python n = 6 dp = [0] (1 << n) dp[0] = 1 for mask in range(1 << n): if dp[mask] == 0: continue for v in…
- What does this program print? python x = 22 cnt = 0 while x: x &= x - 1 cnt += 1 print(cnt, bin(22).count('1'))
- Array [2, 7, 4, 9, 5, 8]. What is the largest sum of a contiguous subarray having at most 3 elements?
- Points (0,0), (1,2), (2,1), (3,3), (4,0) in the plane. How many unordered pairs have Manhattan distance exactly 3?