levo

Elements of Computational Thinking

Thinking ahead

  • 3 things to know
  • Component 02

Overview

Recall questions on identifying inputs and outputs, preconditions and who checks them, caching and its benefits, drawbacks and replacement policies, caching recursive calculations, and the benefits and risks of reusable program components.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

Identify the inputs and outputs of a supermarket self-service checkout.

Key term

Give preconditions for four common operations, and say what happens if each is violated.

Key term

Calculating a Fibonacci number by simple recursion is famously slow. Explain how caching fixes it, and what it costs.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    Identify the inputs and outputs of a supermarket self-service checkout.

    Model answer

    Inputs: barcodes read by the scanner; the mass on the bagging area from the load cell; the mass of the item on the weighing scale for loose produce; the customer's selection of a loose product from the on-screen list; card or contactless payment details from the reader, together with a PIN where required; cash inserted into the note and coin acceptors; the customer's response to prompts such as whether they have a loyalty card or their own bag; a loyalty card scan; and an assistant's key or code when an override is needed. Outputs: the running item list and total on the display; prompts and spoken instructions; the printed receipt; change dispensed; the payment authorisation request sent to the acquirer, and the response acted upon; the attendant-alert light or console message when intervention is needed; the release of the security gate; and the sales and stock records written to the store system. Two distinctions worth making. The product database is not an input in this sense, it is stored data the system consults; and the price displayed is an output derived from processing, not an input, even though it appears on the same screen as the prompts.

  2. 2

    Identify the inputs and outputs of a central heating system with a programmable thermostat, and say what makes this harder than it first looks.

    Model answer

    Inputs: the temperature read from one or more sensors; the target temperature and the schedule set by the user; user commands such as boost, holiday mode or override; the current time from a clock; and, on a connected system, occupancy or geofence signals and outside temperature from a forecast service. Outputs: the switching signal to the boiler and to the circulating pump; signals to individual zone valves or radiator valves; the display of current and target temperature; and log data on run time and energy use. What makes it harder is telling apart three things that all arrive from outside the program: a genuine input that varies during operation, such as the sensor reading; a setting supplied once and then stored, such as the schedule; and a fixed parameter of the design, such as the hysteresis band. The hysteresis matters and is often missed: if the system simply switched on below target and off above it, it would cycle the boiler on and off every few seconds, so the design needs a dead band, for example switching on half a degree below target and off half a degree above. Identifying that need is part of thinking ahead about the situation rather than reading requirements off the description.

  3. 3

    A student lists the output of a library loans system as the book. Why is that wrong, and what is the right way to think about identifying outputs?

    Model answer

    Because the book is a physical object that the system neither produces nor controls; it existed before the transaction and the system merely records what happened to it. An output is something the system emits: data displayed, printed, transmitted, stored or a signal sent to a device. The right way to identify outputs is to ask what leaves the boundary of the system, having first drawn the boundary explicitly. For a loans system the outputs are the loan record written to the database, the due date shown to the borrower and printed on a slip, the reminder email or message sent when the due date approaches, the fine calculated and posted to the borrower's account, the updated availability status seen by other users, the unlock signal to the security gate, and management reports on circulation. The same discipline avoids the mirror-image error on the input side, where students list the borrower rather than the borrower's card number or membership identifier. Two questions make it reliable: what data crosses into the system and from what source, and what data or signal crosses out of it and to what destination.

  4. 4

    What is a precondition, and why identify preconditions before designing a solution?

    Model answer

    A precondition is something that must be true before a process is carried out if that process is to work correctly. It may be a condition on the data, such as the list being sorted or the divisor being non-zero; a condition on the state of the system, such as a file being open or a user being logged in; or a condition on the environment, such as the hardware, network access, permissions or data being available at all. Identifying them in advance matters for three reasons. First, correctness: an algorithm that assumes something unstated will silently give wrong answers when the assumption fails, which is worse than crashing. Second, division of responsibility: once a precondition is written down, it can be agreed whether the caller must guarantee it or the routine must check it, and if that is left implicit it will either be checked twice, wasting effort, or not at all. Third, feasibility: some preconditions are about the project rather than the code, such as whether the data you intend to use actually exists in the required form, whether you have the right to use it, and whether the target hardware can hold it, and discovering that one of those is false after building the solution is expensive.

  5. 5

    Binary search is much faster than a linear search, so why not always use it?

    Model answer

    Because it carries a precondition that a linear search does not: the data must already be in sorted order on the key you are searching by. Binary search works by comparing the target with the middle item and discarding the half that cannot contain it, and that reasoning is only valid if order guarantees where items are. Run it on unsorted data and it does not merely become slower, it returns not found for items that are present, which is a wrong answer rather than a slow one. So the choice depends on the whole situation, not just on the search. If the data arrives unsorted and you search it once, sorting first costs more than the linear scan you were trying to avoid, since a comparison sort is of the order of n log n while the linear search is n. If the data is searched many times between changes, sorting once and searching repeatedly wins easily. If the data is constantly being inserted into, maintaining sorted order has its own cost and a different structure such as a hash table or a balanced tree may beat both. Thinking ahead here means recognising that the precondition has a price and asking who pays it and how often.

  6. 6

    Give preconditions for four common operations, and say what happens if each is violated.

    Model answer

    Popping from a stack: the stack must not be empty. Violated, the operation either returns meaningless data from beyond the top of the structure or raises an underflow error, and in a fixed array implementation an unchecked pop will decrement the top index below zero and corrupt subsequent operations. Dividing: the divisor must not be zero. Violated, integer division raises an error and halts the program, while floating-point division yields infinity or a not-a-number value that then propagates silently through every later calculation. Reading a record from a file: the file must exist, be accessible with the current permissions, and be open in a mode that permits reading. Violated, the read fails, and the common bug is that the failure is ignored and the program continues with an empty or stale buffer. Withdrawing from an account: the account must exist, be open, and hold a balance at least equal to the amount, or have an authorised overdraft. Violated, the account goes negative without authorisation. The pattern is consistent: violating a precondition rarely produces an obviously wrong result at the point of violation, which is why they should be checked explicitly rather than trusted.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Elements of Computational Thinking, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.