Bird
0
0

Find the error in this delivery agent assignment snippet:

medium📝 Analysis Q7 of 15
LLD - Design — Food Delivery System

Find the error in this delivery agent assignment snippet:

def assign_agent(order, agents):
  free_agents = [a for a in agents if a.status == "free"]
  return free_agents[0].id

AReturns agent object instead of ID
BList comprehension syntax is incorrect
CNo check if free_agents list is empty before accessing index 0
DFilters agents by busy status instead of free
Step-by-Step Solution
Solution:
  1. Step 1: Analyze list comprehension

    It correctly filters free agents.
  2. Step 2: Check access to first element

    If no free agents, accessing free_agents[0] causes an error.
  3. Final Answer:

    No check if free_agents list is empty before accessing index 0 -> Option C
  4. Quick Check:

    Check list length before access [OK]
Quick Trick: Always check list is not empty before indexing [OK]
Common Mistakes:
  • Ignoring empty list edge case
  • Misreading list comprehension syntax
  • Confusing status filter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes