Forward, backward, lateral movement in Drone Programming - Time & Space Complexity
When controlling a drone's movement forward, backward, and sideways, it's important to know how the time to complete commands grows as we add more steps.
We want to find out how the number of movement commands affects the total time the drone takes to finish them.
Analyze the time complexity of the following code snippet.
for step in movement_steps:
if step == "forward":
drone.move_forward()
elif step == "backward":
drone.move_backward()
elif step == "left":
drone.move_left()
elif step == "right":
drone.move_right()
else:
drone.hover()
This code makes the drone perform a series of moves based on a list of steps, moving forward, backward, or sideways one step at a time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each movement step and executing one move command.
- How many times: Once for every step in the movement_steps list.
Each additional movement step adds one more command to execute, so the total work grows directly with the number of steps.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 moves |
| 100 | 100 moves |
| 1000 | 1000 moves |
Pattern observation: The total time grows in a straight line as the number of steps increases.
Time Complexity: O(n)
This means the time to complete all movements grows directly in proportion to the number of steps given.
[X] Wrong: "The drone moves all steps instantly, so time doesn't depend on the number of steps."
[OK] Correct: Each step requires a separate command and time to execute, so more steps mean more time.
Understanding how repeated commands affect time helps you explain how drones or robots handle sequences of instructions efficiently.
"What if the drone could combine multiple steps into one command? How would the time complexity change?"
Practice
Solution
Step 1: Understand the direction commands
The commandmove_forward(distance)moves the drone forward by the given distance.Step 2: Match the distance and direction
Since we want to move forward by 5 meters,move_forward(5)is the correct command.Final Answer:
move_forward(5) -> Option BQuick Check:
Forward move = move_forward(5) [OK]
- Confusing forward with backward commands
- Using lateral move commands instead of forward
- Mixing up distance units
Solution
Step 1: Identify correct function syntax
The correct syntax uses the function name followed by parentheses enclosing the distance number.Step 2: Check parameter type and spelling
move_left(3)usesmove_left(3)which is correct.moveLeft(3)uses camelCase which is incorrect here.move_left 3misses parentheses.move_left(three)uses a word instead of a number.Final Answer:
move_left(3) -> Option AQuick Check:
Function call with number in parentheses [OK]
- Omitting parentheses in function call
- Using wrong case in function name
- Passing string instead of number
move_forward(4) move_right(3) move_backward(2) move_left(1)
Solution
Step 1: Track vertical (forward/backward) movement
Start at (0,0). move_forward(4) moves y from 0 to 4. move_backward(2) moves y from 4 to 2.Step 2: Track horizontal (left/right) movement
move_right(3) moves x from 0 to 3. move_left(1) moves x from 3 to 2.Step 3: Calculate final coordinates
Final position is (2, 2).Final Answer:
(2, 2) -> Option CQuick Check:
Horizontal = 3-1=2, Vertical = 4-2=2 [OK]
- Mixing X and Y axes
- Forgetting backward subtracts from Y
- Ignoring left movement subtracts from X
move_forward(5) move_left(2) move_backward() move_right(3)
Solution
Step 1: Check each function call for required arguments
All move commands require a distance number. move_backward() is missing the distance argument.Step 2: Verify other commands
move_left(2), move_forward(5), and move_right(3) all have correct syntax and arguments.Final Answer:
Missing distance argument in move_backward() -> Option AQuick Check:
Functions need distance parameter [OK]
- Calling move functions without distance
- Confusing left and right commands
- Assuming order of commands matters for syntax
Solution
Step 1: Understand square path directions
A square path means moving forward, then right, then backward, then left, each by the same distance.Step 2: Check each option's sequence
move_forward(4) move_right(4) move_backward(4) move_left(4)follows forward, right, backward, left by 4 meters each, completing a square back to start.Step 3: Verify final position
After these moves, the drone returns to (0,0), confirming a square path.Final Answer:
move_forward(4) move_right(4) move_backward(4) move_left(4) -> Option DQuick Check:
Square path = forward, right, backward, left [OK]
- Mixing left and right order
- Not returning to start point
- Using different distances for sides
Ready to prove it?
Pass the test and this pattern moves unknown
