What is the correct order of transaction IDs if sorted by time ascending?
medium
A. ["t1", "t2", "t3"]
B. ["t2", "t3", "t1"]
C. ["t3", "t1", "t2"]
D. ["t2", "t1", "t3"]
Solution
Step 1: Analyze timestamps for each transaction
t2 = 09:00, t1 = 10:00, t3 = 11:00 in UTC time.
Step 2: Sort transactions by ascending time
Order is t2 (earliest), then t1, then t3 (latest).
Final Answer:
["t2", "t1", "t3"] -> Option D
Quick Check:
Sorted by time ascending = [t2, t1, t3] [OK]
Hint: Sort by timestamp ascending for correct order [OK]
Common Mistakes:
Sorting by ID instead of time
Confusing ascending with descending order
Ignoring timestamp format
4. 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?
medium
A. Duplicate transaction
B. KeyError exception
C. No output, transaction added
D. TypeError exception
Solution
Step 1: Check if transaction ID exists in history
The code checks if 't1' is already in the list of IDs in history.
Step 2: Since 't1' exists, print duplicate message
The else branch runs and prints "Duplicate transaction".
Final Answer:
Duplicate transaction -> Option A
Quick Check:
Duplicate ID detected = print message [OK]
Hint: Check for existing ID before adding to avoid duplicates [OK]
Common Mistakes:
Assuming transaction is added anyway
Expecting an exception instead of print
Confusing list comprehension syntax
5. You want to design a scalable transaction history system for millions of users. Which approach best ensures fast retrieval of a user's transactions sorted by time?
hard
A. Store transactions in separate files per day without indexing
B. Store all transactions in one big list and scan it every time
C. Use a database with an index on user ID and timestamp
D. Keep transactions only in memory without persistence
Solution
Step 1: Consider scalability and retrieval speed
Scanning one big list or files without index is slow for millions of users.
Step 2: Use database indexing on user ID and timestamp
This allows fast queries to get transactions per user sorted by time efficiently.
Step 3: Avoid in-memory only storage for persistence and scale
Memory-only storage risks data loss and limits scale.
Final Answer:
Use a database with an index on user ID and timestamp -> Option C
Quick Check:
Indexing = fast retrieval at scale [OK]
Hint: Index on user ID and timestamp for fast queries [OK]