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?- A. True False False
- B. False False True
- C. True False True
- 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
- Problem: Builds 1..100 are tested; every build from some version onward is broken. isbad() is expensive, so find the first bad build with…
- Problem: Sort values by increasing frequency; values with the same frequency go in decreasing order. python from collections import…
- Problem: A researcher's h-index is the largest h such that at least h papers have ≥ h citations each. python def hindex(c): c = sorted(c…
- Problem: A 3 × 4 matrix is sorted row-wise and each row starts after the previous row ends. Search it as one virtual sorted array of R·C…
- Problem: A truck holds at most 8 boxes. Each box type is (count, units per box). Maximise the total units loaded. python def…
- Problem: Reorder the letters of s so that letters listed in order appear in that order; letters not in order go last, keeping their…