Tracing Code
Loop logic
100

The Scenario: You have a storage bin called Inventory that starts with 10 items. You process a list of delivery tickets one by one: [+5, -12, +8, -2].

  • Rule A: If a ticket is positive, add that many items to the Inventory.
  • Rule B: If a ticket is negative, subtract that many items, but only if the Inventory has enough. If there aren't enough items, ignore the ticket entirely.

Question: What is the final number of items in the Inventory after all four tickets are processed?

21

100

The Scenario: You have a penny bank that starts with 5 pennies. You decide to add 3 pennies every day. You have a rule: "Keep adding pennies as long as the total is less than 15."

Question:

  1. How many days will you add pennies?
  2. What is the final number of pennies in the bank when you stop?

4 days and 17 pennies

200

The Scenario: You start with the word "SPACE". You must apply these three rules to every letter, starting from the left:

  1. Skip the first letter entirely.
  2. For the next letter, replace it with the letter that comes two places after it in the alphabet (e.g., A becomes C).
  3. For the letter after that, remove it from the word entirely.
  4. Repeat steps 2 and 3 until you reach the end of the word.

Question: What is the final "word" (or string of letters) left at the end?

SCC

200

The Scenario: A climber starts at the bottom of a 10-step staircase (Step 0). Their goal is to reach Step 10. They follow these rules for every "turn" until they reach or pass Step 10:

  • Step A: Move up 2 steps.
  • Step B: If the step they are currently standing on is a multiple of 3 (like 3, 6, or 9), they must slide back 1 step.
  • Step C: Check if they are at or above Step 10. If not, start the next turn.

Question: How many "turns" does it take for the climber to reach Step 10? List the step they are on at the end of each turn.

6 turns 2, 4, 5, 7, 8, 10

300

The Scenario: A robot is on a grid at position (0, 0). It is given a "Move Command": Repeat 3 times: [Step Right, Step Up]. However, a "Global Interruption" rule is active:

  • The Interruption: Every time the robot's Up position is an even number (like 0 or 2), it must take one extra Step Right immediately after its normal movement.

Question: Trace the robot's journey. What is its final coordinate (Right, Up) after the "Repeat 3" command is finished?

6, 3

300

The Scenario: A librarian is checking 3 shelves. Each shelf has 4 books. The librarian must follow this "Nested Routine":

  • Outer Loop: Move to the next shelf (Shelf 1, then 2, then 3).
  • Inner Loop: For every book on that shelf, check its condition.
    • If it is the 2nd book on any shelf, it takes 2 minutes to check.
    • For all other books, it takes 1 minute to check.
  • Special Rule: After finishing each shelf, the librarian takes a 3-minute break before moving to the next one.

Question: How many total minutes does the librarian spend on this routine, including the check times and breaks?

21 total minutes