Bird
Raised Fist0
Drone Programmingprogramming~20 mins

Distributed task allocation in Drone Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Distributed Task Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of task assignment in distributed drones

Consider a system where drones pick tasks based on their battery level and distance. What is the output of the following code snippet that assigns tasks to drones?

Drone Programming
drones = [{'id': 'D1', 'battery': 80, 'distance': 5}, {'id': 'D2', 'battery': 50, 'distance': 3}, {'id': 'D3', 'battery': 90, 'distance': 10}]
tasks = ['Survey', 'Delivery', 'Mapping']
assignments = {}
for task, drone in zip(tasks, sorted(drones, key=lambda d: (d['distance'], -d['battery']))):
    assignments[drone['id']] = task
print(assignments)
A{"D2": "Survey", "D3": "Delivery", "D1": "Mapping"}
B{"D2": "Survey", "D1": "Delivery", "D3": "Mapping"}
C{"D1": "Survey", "D2": "Delivery", "D3": "Mapping"}
D{"D3": "Survey", "D1": "Delivery", "D2": "Mapping"}
Attempts:
2 left
💡 Hint

Look at how drones are sorted by distance ascending and battery descending.

🧠 Conceptual
intermediate
1:30remaining
Understanding consensus in distributed task allocation

In a distributed drone network, what is the main purpose of a consensus algorithm during task allocation?

ATo reduce the number of drones in the network
BTo maximize the battery usage of a single drone
CTo randomly assign tasks without communication
DTo ensure all drones agree on which tasks are assigned to which drones
Attempts:
2 left
💡 Hint

Consensus means agreement among multiple parties.

🔧 Debug
advanced
2:00remaining
Identify the error in task allocation code

What error will this code produce when run in a distributed drone system?

tasks = ['Inspect', 'Deliver']
drones = [{'id': 'D1'}, {'id': 'D2'}]
assignments = {}
for i in range(len(tasks)):
    assignments[drones[i]['id']] = tasks[i]
assignments[drones[2]['id']] = 'Charge'
print(assignments)
AIndexError: list index out of range
BKeyError: 'id'
CTypeError: unsupported operand type(s)
DNo error, prints {'D1': 'Inspect', 'D2': 'Deliver', 'D3': 'Charge'}
Attempts:
2 left
💡 Hint

Check the index used to access drones in the last assignment.

📝 Syntax
advanced
1:30remaining
Syntax error in distributed task allocation code

Which option contains the correct syntax to create a dictionary mapping drone IDs to their assigned tasks?

tasks = ['Scan', 'Deliver']
drones = [{'id': 'D1'}, {'id': 'D2'}]
Aassignments = {drone['id'] -> task for drone, task in zip(drones, tasks)}
Bassignments = {drone['id'] = task for drone, task in zip(drones, tasks)}
Cassignments = {drone['id']: task for drone, task in zip(drones, tasks)}
Dassignments = {drone['id'] task for drone, task in zip(drones, tasks)}
Attempts:
2 left
💡 Hint

Dictionary comprehensions use colon ':' to separate keys and values.

🚀 Application
expert
2:30remaining
Calculate total tasks assigned after distributed allocation

Given the following code simulating distributed task allocation, what is the total number of tasks assigned after execution?

tasks = ['A', 'B', 'C', 'D']
drones = [{'id': 'D1', 'capacity': 2}, {'id': 'D2', 'capacity': 1}, {'id': 'D3', 'capacity': 3}]
assignments = {d['id']: [] for d in drones}
task_index = 0
while task_index < len(tasks):
    for drone in drones:
        if len(assignments[drone['id']]) < drone['capacity'] and task_index < len(tasks):
            assignments[drone['id']].append(tasks[task_index])
            task_index += 1
        if task_index >= len(tasks):
            break
count = sum(len(v) for v in assignments.values())
print(count)
A4
B3
C5
D6
Attempts:
2 left
💡 Hint

Count how many tasks are assigned before all tasks run out.

Practice

(1/5)
1. What is the main goal of distributed task allocation in drone programming?
easy
A. To share work fairly among drones without a central controller
B. To make one drone do all the tasks quickly
C. To randomly assign tasks without any order
D. To stop drones from communicating with each other

Solution

  1. Step 1: Understand distributed task allocation

    It means dividing tasks so all drones get work fairly.
  2. Step 2: Identify the goal in drone programming

    The goal is to avoid a single controller and let drones share tasks efficiently.
  3. Final Answer:

    To share work fairly among drones without a central controller -> Option A
  4. Quick Check:

    Distributed task allocation = fair work sharing [OK]
Hint: Remember: share tasks fairly, no single boss drone [OK]
Common Mistakes:
  • Thinking one drone does all tasks
  • Assuming tasks are assigned randomly
  • Believing drones don't communicate
2. Which of the following is the correct way to assign tasks to drones in a round-robin style using Python?
easy
A. task_index * len(drones)
B. len(drones) % task_index
C. task_index + len(drones)
D. task_index % len(drones)

Solution

  1. Step 1: Understand round-robin assignment

    Round-robin cycles through drones by using modulo with drone count.
  2. Step 2: Apply modulo operator correctly

    Using task_index % len(drones) cycles task indices over drone list indices.
  3. Final Answer:

    task_index % len(drones) -> Option D
  4. Quick Check:

    Modulo with drone count cycles tasks [OK]
Hint: Use modulo (%) with drone count to cycle tasks [OK]
Common Mistakes:
  • Reversing modulo operands
  • Adding or multiplying instead of modulo
  • Using modulo with wrong variable
3. What will be the output of this code snippet?
tasks = ['scan', 'deliver', 'map', 'charge']
drones = ['droneA', 'droneB']
assignments = {}
for i, task in enumerate(tasks):
    drone = drones[i % len(drones)]
    assignments.setdefault(drone, []).append(task)
print(assignments)
medium
A. {'droneA': ['scan', 'map'], 'droneB': ['deliver', 'charge']}
B. {'droneA': ['scan', 'deliver'], 'droneB': ['map', 'charge']}
C. {'droneA': ['scan'], 'droneB': ['deliver', 'map', 'charge']}
D. {'droneA': ['charge'], 'droneB': ['scan', 'deliver', 'map']}

Solution

  1. Step 1: Assign tasks using modulo

    Tasks are assigned to drones cycling through drones list by i % 2.
  2. Step 2: Map tasks to drones

    Index 0: 'scan' -> droneA, 1: 'deliver' -> droneB, 2: 'map' -> droneA, 3: 'charge' -> droneB.
  3. Final Answer:

    {'droneA': ['scan', 'map'], 'droneB': ['deliver', 'charge']} -> Option A
  4. Quick Check:

    Modulo cycles tasks evenly [OK]
Hint: Match task index modulo drone count to assign tasks [OK]
Common Mistakes:
  • Mixing task order in assignments
  • Ignoring modulo cycling
  • Appending tasks to wrong drone
4. Identify the error in this drone task allocation code:
drones = ['D1', 'D2', 'D3']
tasks = ['photo', 'survey', 'deliver']
assignments = {}
for i in range(len(tasks)):
    drone = drones[i // len(drones)]
    assignments.setdefault(drone, []).append(tasks[i])
print(assignments)
medium
A. assignments.setdefault is used incorrectly
B. Using range(len(tasks)) is incorrect for looping
C. Using integer division (//) instead of modulo (%) assigns all tasks to the first drone
D. The drones list should be empty

Solution

  1. Step 1: Check drone index calculation

    Using i // len(drones) results in index 0 for i=0,1,2 (len(drones)=3), assigning all tasks to 'D1' instead of cycling round-robin.
  2. Step 2: Identify correct operator

    Modulo (%) should be used to cycle through drone indices safely.
  3. Final Answer:

    Using integer division (//) instead of modulo (%) assigns all tasks to the first drone -> Option C
  4. Quick Check:

    Use modulo (%) to cycle indices [OK]
Hint: Use % not // to cycle through drone indices [OK]
Common Mistakes:
  • Confusing // with %
  • Thinking range(len(tasks)) is wrong
  • Misusing setdefault method
5. You have 3 drones and 7 tasks. You want to assign tasks so each drone gets tasks in a round-robin way but skip any task named 'wait'. Which code snippet correctly implements this?
hard
A. assignments = {} for i, task in enumerate(tasks): if task == 'wait': continue drone = drones[len(drones) % i] assignments.setdefault(drone, []).append(task)
B. assignments = {} for i, task in enumerate(tasks): drone = drones[i % len(drones)] if task != 'wait': assignments.setdefault(drone, []).append(task)
C. assignments = {} for task in tasks: if task == 'wait': continue drone = drones[tasks.index(task) % len(drones)] assignments.setdefault(drone, []).append(task)
D. assignments = {} for i in range(len(tasks)): if tasks[i] == 'wait': continue drone = drones[i // len(drones)] assignments.setdefault(drone, []).append(tasks[i])

Solution

  1. Step 1: Filter out 'wait' tasks correctly

    Check task != 'wait' before assigning to drones.
  2. Step 2: Assign tasks using modulo for round-robin

    Use i % len(drones) to cycle drones correctly.
  3. Step 3: Avoid using index() inside loop to prevent errors with duplicates

    Using tasks.index(task) can cause wrong drone assignment if tasks repeat.
  4. Final Answer:

    assignments = {} for i, task in enumerate(tasks): drone = drones[i % len(drones)] if task != 'wait': assignments.setdefault(drone, []).append(task) -> Option B
  5. Quick Check:

    Filter first, then assign with modulo [OK]
Hint: Check task before assign; use modulo for drone cycling [OK]
Common Mistakes:
  • Using integer division instead of modulo
  • Using tasks.index() causing wrong assignments
  • Not skipping 'wait' tasks properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard