Data Structures & AlgorithmsEasyTCS NQT Digital · Product Company SDE

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 customer can get correct change.

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 customer can get correct change.
def change_ok(bills):
    five = ten = 0
    for b in bills:
        if b == 5: five += 1
        elif b == 10:
            if not five: return False
            five -= 1; ten += 1
        else:
            if ten and five: ten -= 1; five -= 1
            elif five >= 3: five -= 3
            else: return False
    return True
print(change_ok([5, 5, 10, 5, 20]), change_ok([5, 5, 5, 20, 10, 20]), change_ok([5, 5, 5, 5, 10, 20, 10, 10]))
What is printed?
  1. A. True False False
  2. B. False False True
  3. C. True False True
  4. D. True True True

Correct answer: C. True False True

For ₹15 change prefer one ₹10 + one ₹5: ₹5 notes are more versatile (they are needed for ₹10 customers too). Second list: the 20 uses all three 5s, so the next 10-payer gets no change → False. Third list: paying the 20 with 10 + 5 leaves two 5s for the later 10-payers → True; paying it with 5 + 5 + 5 would leave none and fail. O(n).

More Coding Interview Problems questions

Exams that ask this topic