Algorithmic Thinking MCQs for NSCT
Algorithmic thinking is the skill of turning a task into a finite list of precise steps, then checking those steps by hand with dry runs and trace tables. It is one of the 16 official topics in Problem Solving and Analytical Skills, which carries 20% of the NSCT according to HEC's Areas of Competencies. Questions here usually give you a short piece of code or pseudocode and ask what it prints, how many times a loop runs, or which input breaks it.
This topic builds on Algorithms & Flow Control, which covers how to read pseudocode and flowcharts. Here the focus is on designing steps and checking them.
Step design
An algorithm is a finite sequence of well-defined steps that takes input and produces the required output. Knuth lists five properties:
| Property | Meaning | Breaks when… |
|---|---|---|
| Finiteness | Stops after a finite number of steps | A loop condition can never become false |
| Definiteness | Every step is precise and unambiguous | A step says "add a few numbers" |
| Input | Takes zero or more inputs | – |
| Output | Produces at least one output | Nothing is returned or printed |
| Effectiveness | Each step is basic enough to carry out exactly | A step needs an unbounded search with no method |
Good step design uses top-down design (stepwise refinement): write the task as 3–5 big steps, then refine each until it is simple enough to code. Variables play standard roles: a counter adds 1 per event, an accumulator builds a running total, a flag records a yes/no condition, and a sentinel is a special input value that marks the end of data.
Exam angle: ordering questions test dependencies. A step can only run once every value it uses has been computed, so "divide by N" must come after "add up all N values".
Dry runs
A dry run is executing an algorithm by hand with chosen test values, following each step exactly as written rather than as you think it should work. Choose inputs that test the boundaries, known as edge cases:
- an empty input and a single-element input;
- values that break hidden assumptions, such as all-negative numbers when the code starts its maximum at 0;
- the exact value where a loop condition changes, such as
i == n.
Exam angle: "which input exposes the bug?" questions are dry-run questions. Try each option on the code; the right answer is the one whose output differs from what the specification says.
Trace tables
A trace table records a dry run. It has one column per variable (and one for output if useful) and one row for each step or loop iteration. This code adds the even numbers in a list:
a = [3, 4, 7, 10]
total = 0
for x in a:
if x % 2 == 0:
total = total + x
print(total)
| x | x % 2 == 0? | total |
|---|---|---|
| – | – | 0 |
| 3 | False | 0 |
| 4 | True | 4 |
| 7 | False | 4 |
| 10 | True | 14 |
It prints 14. Writing the table forces you to record values you would otherwise "remember" wrongly, especially in nested loops and when variables are updated together.
Exam angle: watch the last iteration. Most trace mistakes happen where the loop condition is checked one final time, for example range(1, 5) stops at 4, not 5.
Correctness and termination
A dry run shows that an algorithm works for the inputs you tried; it does not prove it works for all inputs. Testing shows the presence of bugs, not their absence (Dijkstra). For a proof, CLRS uses a loop invariant, a condition that holds before the loop and after every iteration, checked in three parts: initialisation, maintenance and termination. To show that a loop stops, find a quantity that moves towards a bound on every iteration, such as high − low in binary search.
Exam angle: a loop with while i != 10 and a step of 3 never stops, because i jumps from 9 to 12. Prefer < or > conditions.
Common traps
- Off-by-one ranges.
range(1, len(a))skips index 0, andrange(n)stops at n − 1. - Overwritten values.
a = bfollowed byb = acopies one value into both variables. - Flags reset inside the loop.
found = (x == t)reports only the last comparison. - Wrong starting value. Starting a maximum at 0 fails for all-negative lists.
- Condition order. In an if/elif chain, the first true branch wins, so test the most specific case (such as divisible by 15) first.
Worked examples
Understanding. An algorithm's last step is "go back to step 1", and no step ever ends the process. Which property does it break?
Answer: finiteness. Every step may be precise, but the procedure never terminates, so it is not an algorithm in Knuth's sense.
Application. What does this print?
x = 2
for i in range(3):
x = x * x
print(x)
Answer: 256. The trace for x is 2 → 4 → 16 → 256. The loop squares x three times; it does not multiply by 2 or 3.
Analysis. This code should count marks strictly above 50. It prints 3. What is wrong?
count = 0
for m in [50, 70, 40, 90]:
if m >= 50:
count = count + 1
print(count)
Answer: the condition should be m > 50. With >=, the mark 50 is counted too. Only 70 and 90 are strictly above 50, so the correct output is 2. Testing with a value exactly on the boundary (50) is what exposes the bug.
Practise
The quiz below has 99 original questions, filterable by tier (understanding, application, analysis) and difficulty. Every code output in it was checked by running the code.
Practise: 99 questions
FAQs
Is algorithmic thinking part of the NSCT syllabus?
Yes. HEC lists Algorithmic Thinking as one of the 16 topics under Problem Solving and Analytical Skills in its Areas of Competencies. Expect short code or pseudocode traces, loop counts and step-ordering questions rather than long programs.
How do I trace code faster under time pressure?
Write a mini trace table in the margin with one column per changing variable. Update only the values that change in each step, and mark the moment the loop condition becomes false. With practice you will spot patterns, such as a loop that halves its variable running about log₂n times.
Which language do trace questions use?
The questions on this site use Python and plain pseudocode, because they read clearly for students from any programming background. The tracing method is the same in C++ or Java: follow each assignment in order, and remember that integer division and loop bounds can differ slightly between languages.
What is the difference between a dry run and debugging?
A dry run is a planned walk-through of the logic with chosen inputs, often done before the code runs at all. Debugging starts from a known wrong result and searches for its cause. Dry runs are one of the main tools you use while debugging, which is covered in Debugging & Error Analysis.
Next step
Move on to Critical Thinking & Decision Making MCQs, go back to the Problem Solving hub, or time yourself with an NSCT mock test.