Bird
0
0

Consider this pseudocode for assigning an agent:

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

Consider this pseudocode for assigning an agent:
for agent in agents:
  if agent.status == 'free' and distance(agent, order) < min_distance:
    min_distance = distance(agent, order)
    assigned_agent = agent
return assigned_agent

What will this code return if all agents are busy?

AThe closest free agent
BNone or null
CThe first agent in the list
DAn error due to undefined variable
Step-by-Step Solution
Solution:
  1. Step 1: Check variable initializations

    The code does not initialize assigned_agent or min_distance before the loop.
  2. Step 2: Trace execution when all agents are busy

    The if condition's first part (agent.status == 'free') fails for all agents, so due to short-circuit evaluation of 'and', the second part (distance < min_distance) is never evaluated. The loop ends without ever setting assigned_agent. Returning an uninitialized assigned_agent causes an error due to undefined variable.
  3. Final Answer:

    An error due to undefined variable -> Option D
  4. Quick Check:

    No initialization + all busy = undefined variable error [OK]
Quick Trick: No variable initialization leads to undefined variable error [OK]
Common Mistakes:
  • Thinking assigned_agent defaults to None or null
  • Assuming it returns the first agent regardless of status
  • Believing the code handles no free agents gracefully

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes