Communication between drones in Drone Programming - Time & Space Complexity
When drones communicate, they send messages back and forth. We want to know how the time to complete this communication changes as more drones join the network.
How does the number of drones affect the time it takes for all messages to be shared?
Analyze the time complexity of the following code snippet.
for drone in drones:
for other_drone in drones:
if drone != other_drone:
drone.send_message(other_drone, message)
This code makes each drone send a message to every other drone in the group.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sending a message from one drone to another.
- How many times: For each drone, it sends messages to all other drones, so this happens many times inside two loops.
As the number of drones grows, the total messages sent grow much faster because each drone talks to all others.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 90 messages |
| 100 | 9,900 messages |
| 1000 | 999,000 messages |
Pattern observation: When the number of drones doubles, the messages sent roughly quadruple.
Time Complexity: O(n²)
This means the time to send all messages grows very quickly as more drones join, because each drone talks to every other drone.
[X] Wrong: "Each drone only sends one message, so time grows linearly with drones."
[OK] Correct: Actually, each drone sends messages to all others, so the total messages grow much faster than the number of drones.
Understanding how communication scales helps you design better drone networks and shows you can think about how programs behave as they grow.
"What if each drone only sends messages to its nearest 3 neighbors? How would the time complexity change?"
Practice
Solution
Step 1: Understand drone communication purpose
Drones communicate to share data and work together efficiently.Step 2: Identify correct purpose from options
Only To share important information and coordinate tasks mentions sharing information and coordination, which matches the purpose.Final Answer:
To share important information and coordinate tasks -> Option AQuick Check:
Communication = Share info and coordinate [OK]
- Thinking drones recharge each other
- Confusing communication with appearance changes
- Assuming drones compete instead of cooperate
Solution
Step 1: Understand message sending syntax
In drone_programming, a drone sends a message by calling send_message, and the receiver uses receive_message.Step 2: Check which option matches this syntax
droneB.receive_message(droneA.send_message('Hello'))shows droneA sending and droneB receiving correctly.Final Answer:
<code>droneB.receive_message(droneA.send_message('Hello'))</code> -> Option BQuick Check:
Send then receive method call =droneB.receive_message(droneA.send_message('Hello'))[OK]
- Using wrong method names
- Trying to chain send_message with .to()
- Incorrect function call order
droneA.inbox = []
droneB.inbox = []
message = droneA.send_message('Status update')
droneB.receive_message(message)
print(droneB.inbox)Solution
Step 1: Trace message sending and receiving
droneA sends 'Status update', droneB receives it and adds to inbox.Step 2: Check droneB's inbox content after receiving
Inbox contains one message: ['Status update'].Final Answer:
['Status update'] -> Option AQuick Check:
Inbox after receive_message = ['Status update'] [OK]
- Assuming inbox stays empty
- Thinking message duplicates
- Expecting attribute errors
droneA.inbox = []
droneB.inbox = []
message = droneA.send_message('Ready')
droneB.receive_message(message)
print(droneA.inbox)Solution
Step 1: Analyze message flow
droneA sends message, droneB receives it, so droneB's inbox changes, not droneA's.Step 2: Check printed inbox
Printing droneA.inbox shows empty because droneA did not receive any message.Final Answer:
droneA.inbox is printed but droneA never received messages -> Option DQuick Check:
Inbox printed is sender's, but sender has no received messages [OK]
- Confusing sender and receiver inboxes
- Missing parentheses on method calls
- Wrong drone calling receive_message
def process_inbox(droneC):
for msg in droneC.inbox:
if msg.sender_id == 'droneA':
print(f"Processing: {msg.content}")
else:
print("Ignoring message")Solution
Step 1: Understand message filtering logic
Filtering by sender_id ensures only messages from droneA are processed.Step 2: Verify code matches filtering goal
Code checks msg.sender_id == 'droneA' and processes accordingly, which is correct.Final Answer:
The code correctly checks sender_id before processing messages -> Option CQuick Check:
Filter by sender_id = correct filtering [OK]
- Checking receiver_id instead of sender_id
- Trying to delete messages in else block unnecessarily
- Iterating wrong drone's inbox
Ready to prove it?
Pass the test and this pattern moves unknown
