Data Structures & AlgorithmsVery hardProduct company OA
Problem: A council of two parties, R and D, votes in rounds in seat order. On their turn a member bans the next opposing member who has not yet acted (and still has a voice); the banned member never acts again. The party that remains wins. Simulate with two queues of seat indices.
Problem: A council of two parties, R and D, votes in rounds in seat order. On their turn a member bans the next opposing member who has not yet acted (and still has a voice); the banned member never acts again. The party that remains wins. Simulate with two queues of seat indices.
from collections import deque
def council(s):
r = deque(i for i, c in enumerate(s) if c == "R")
d = deque(i for i, c in enumerate(s) if c == "D")
n = len(s)
while r and d:
a, b = r.popleft(), d.popleft()
if a < b: r.append(a + n)
else: d.append(b + n)
return "R" if r else "D"
print(council("RDD"), council("DRRDR"), council("DDRRR"))What is printed?- A. D R D
- B. R R D
- C. D D D
- D. D R R
Correct answer: A. D R D
Compare the earliest member of each party: whoever acts first bans the other and re-joins for the next round with index + n. "RDD": R0 bans D1, but D2 acts before R's next turn (index 3) and bans R → D. "DRRDR" → R. "DDRRR": D0 bans R2, D1 bans R3, R4 bans D5 (= D0's next turn), then D6 (D1) bans R9 → D. Majority alone does not decide it (R has 3 of 5 in "DDRRR"). O(n) rounds overall.
More Coding Interview Problems questions
- Problem: People stand in a line facing right with distinct heights. Person i can see person j > i if everyone strictly between them is…
- Problem: An employee's day is 'tiring' if they worked more than 8 hours. Find the longest run of consecutive days in which tiring days…
- Problem: A sorted list of ticket prices is searched with Python's bisect module. python from bisect import bisectleft, bisectright a =…
- Problem: Orders (name, priority) are sorted by priority only, using Python's sorted with a key. python orders = [("ravi", 3), ("asha", 1)…
- Problem: A juice stall sells a ₹5 cup; customers pay with ₹5, ₹10 or ₹20 notes and the stall starts with no change. Decide whether every…
- Problem: Builds 1..100 are tested; every build from some version onward is broken. isbad() is expensive, so find the first bad build with…