Bird
Raised Fist0

You have this code snippet to add a transaction record:

medium📝 Analysis Q14 of Q15
LLD - Design — Splitwise (Expense Sharing)
You have this code snippet to add a transaction record:
def add_transaction(history, transaction):
    if transaction['id'] not in [t['id'] for t in history]:
        history.append(transaction)
    else:
        print("Duplicate transaction")

history = [{"id": "t1"}]
add_transaction(history, {"id": "t1"})

What is the output when running this code?
ADuplicate transaction
BKeyError exception
CNo output, transaction added
DTypeError exception
Step-by-Step Solution
Solution:
  1. Step 1: Check if transaction ID exists in history

    The code checks if 't1' is already in the list of IDs in history.
  2. Step 2: Since 't1' exists, print duplicate message

    The else branch runs and prints "Duplicate transaction".
  3. Final Answer:

    Duplicate transaction -> Option A
  4. Quick Check:

    Duplicate ID detected = print message [OK]
Quick Trick: Check for existing ID before adding to avoid duplicates [OK]
Common Mistakes:
MISTAKES
  • Assuming transaction is added anyway
  • Expecting an exception instead of print
  • Confusing list comprehension syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes