← Fundamentals
Fundamentals

Tracing a small program

Does this worksheet describe the quote that actually ran?

Describe one precise moment of execution before drawing a conclusion from the final result.

Outcome

Keep current locals, suspended callers and already printed output separate at a precise source checkpoint.

Terminology (4)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Source checkpoint
A named before/after boundary in the supplied source. It is a worksheet location, not evidence that a debugger captured a pause.
Invocation
One execution of a function, with its own parameters, locals and return destination.
Suspended caller
A calling execution waiting for a callee result before its surrounding operation can finish.
Output prefix
The text produced by completed output statements at a chosen boundary. Later values do not rewrite that text. This worksheet does not measure when buffered text reaches a terminal.

State at an execution boundary

A trace records execution as a sequence of chosen boundaries. At each boundary, identify the operation that comes next, the values already established and the output already produced. A source line can contain several operations, so say whether the checkpoint is before or after the operation being discussed.

Each function invocation owns its parameters and local values. A caller can be waiting for a helper to return while the helper has already computed its own result. Nested calls can also have separate locals with identical names. Keep an entry for each active invocation and resume the waiting caller only when its call finishes.

01Next operation

Check the row before changing the fee calculation

A support engineer is documenting an order-pricing reproduction. The diagnostic reaches the expected final quote, but the worksheet says the fee helper still holds the old subtotal. Before changing the arithmetic, check whether the row describes the right moment and invocation.

The reduced diagnostic reads a base amount and one fee in integer minor units. Use valid input 2 4; input validation and overflow are outside this reproduction. The proposed P row says subtotal 2 and includes the add line in its output. P is immediately before the marked output statement. Which part of that row can the completed addition justify?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFileSync } from "node:fs";

function add(base: number, amount: number): number {
    const subtotal = base + amount;

    // Checkpoint P: immediately before this output operation.
    console.log("add", subtotal);

    return subtotal;
}

function main() {
    const [base, amount] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
    let total = base;
    console.log("start", total);

    total = add(base, amount);

    console.log("done", total);
}

main();
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "2 4\n"

At P with input 2 4, which correction belongs in the helper row?

02Pending caller

A corrected helper row does not complete the caller assignment

The local row is corrected. The next proposed row copies that subtotal into main’s total at P. Keep the same input and source: main called add to obtain the right-hand side of an assignment. Has that assignment completed while add is still at P?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFileSync } from "node:fs";

function add(base: number, amount: number): number {
    const subtotal = base + amount;

    // Checkpoint P: immediately before this output operation.
    console.log("add", subtotal);

    return subtotal;
}

function main() {
    const [base, amount] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
    let total = base;
    console.log("start", total);

    total = add(base, amount);

    console.log("done", total);
}

main();
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "2 4\n"

At P, which caller value and output prefix belong in the worksheet?
Loading Computed here, still waiting there…

03Invocation identity

Name the fee-group invocation when the source line repeats

The full reproduction contains nested fee groups, so a source line alone is no longer enough to label a row. Use input 1 2 1: base 1, an outer fee 2 and an inner fee 1. The supplied setup builds that chain. quote logs its base and remaining group count, applies one fee, then calls quote on the inner group. An absent group returns the accumulated base. Review Q in the outer quote invocation, after its recursive child has returned but before its resume output.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFileSync } from "node:fs";

function add(base: number, amount: number): number {
  const subtotal = base + amount;

  // Checkpoint P: immediately before this output operation.
  console.log("add", subtotal);

  return subtotal;
}

type FeeGroup = {
  fee: number;
  remaining: number;
  inner: FeeGroup | null;
};

function quote(base: number, group: FeeGroup | null): number {
  const remaining = group?.remaining ?? 0;
  console.log("enter", base, remaining);
  if (group === null) return base;

  const child = quote(add(base, group.fee), group.inner);

  // Checkpoint Q: immediately before this output operation.
  console.log("resume", base, child);

  return child;
}

function main() {
  const [base, ...fees] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);
  let group: FeeGroup | null = null;
  for (let i = fees.length - 1; i >= 0; i--) {
    group = { fee: fees[i], remaining: fees.length - i, inner: group };
  }

  let total = base;
  console.log("start", total);

  total = quote(base, group);

  console.log("done", total);
}

main();
TypeScript terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc main.ts --noEmit --skipLibCheck --target es2022 --module nodenext --types node && node main.ts

Standard input (escaped): "1 2 1\n"

At outer Q with input 1 2 1, which invocation row and output ending are current?

04Condition checks

Keep the last condition check in the trace

A worksheet for an iterative pricing pass needs condition rows as well as value rows. It can process the same fees without nested calls. Start with total 4, fees [2, 1] and index 0. While index is less than the list length, add fees[index] to total and advance index. The condition is checked before each body execution and once more before exit. The final false check is a step even though no body follows it.

Worksheet for the explicit fee-list loop
Boundarytotalindex
Condition true40
Body complete61
Condition true61
Body complete72
Condition false; exit72

This is an authored worksheet for the stated loop behavior, not a captured debugger session. Choose smaller boundaries when the distinction being investigated lies inside a body statement.

Try one change

In a local copy of the first program, move the final output statement in main to immediately before the assignment that calls add. Predict the complete output prefix at P, then run the displayed native command with the same input. Record the changed statement boundary and keep the helper’s locals separate from main’s destination.

Check this idea later
A quote starts at 900 and calls add with a fee of -50. Stop immediately after add’s output statement but before its return. Which row is current?
Two nested groups each charge zero. Both quote invocations therefore have the same base. What must a Q worksheet label include to distinguish them?

05Practice

Complete the support worksheet for another order

Finish the pricing reproduction’s worksheet for fresh orders. First locate a fee helper’s computed local. Then stop at an inner return while its parent is still waiting. Finally follow three fees, including a discount, to a helper that has not printed yet. A matching final quote cannot substitute for these intermediate rows.

Use the supplied source to correct the worksheet fields. The lab assesses source-based tracing decisions; its checkpoints are authored questions, not captured debugger pauses.

Review the pricing support note

The same nested-group program covers a single fee, an inner return and a three-fee order with a discount.

  1. easy · F-TRACE-V4-1 Check the first fee row
  2. medium · F-TRACE-V4-2 Record an inner return
  3. hard · F-TRACE-V4-3 Follow the discount and final fee
Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.