Question 1[14 marks]
A cinema lets people book seats online.
The Lumen is a small cinema with one screen of 8 rows, each of 12 seats. It wants an online system that lets customers book seats for its films.
(a) Outline how decomposition could be applied to the design of this booking system. Name two sub-problems in your answer. [3]
(b) The part of the system that lets customers pick seats stores each row as a list of 0s and 1s, where 0 is a free seat and 1 is a booked seat. Identify one detail of the real cinema or its customers that this leaves out, and outline why leaving it out helps. [2]
[5]
- p1 [1]
(a) Says decomposition breaks the whole booking problem into smaller sub-problems that can each be designed, written and tested on their own.
- p2 [1]
(a) Names one sub-problem of this system, such as choosing a showing, showing the seat plan and picking seats, taking the card payment, or making and emailing the e-ticket.
- p3 [1]
(a) Names a second, different sub-problem of this system from the same kind of list.
- p4 [1]
(b) Identifies a detail the 0s and 1s leave out, such as who booked a seat, the price of a seat, what the seat looks like or is made of, or how far the seat is from the screen.
- p5 [1]
(b) Says leaving it out helps because the seat-picking part only needs to know whether each seat is free, so the data is smaller and the code that searches it is simpler to write and check.
Also accept
- For p1, "splitting the problem into smaller, more manageable parts that can be worked on separately (or by different people)" earns the mark.
- For p2 and p3, any two distinct parts of this booking system earn the marks, for example logging in or creating an account, listing films and showing times, or sending a reminder email.
- For p4, the seat's row letter as printed on the ticket earns the mark, since only a position in a list is kept.
- For p5, "the programmer can concentrate on the one thing that matters for picking seats" earns the mark.
No marks for
- For p2 and p3, steps that are not parts of this system, such as "input", "process" and "output", or "build the cinema".
- For p2 and p3, naming the same sub-problem twice in different words, such as "payment" and "taking the card details".
- For p4, saying it leaves out whether the seat is booked. That is exactly what the 0s and 1s keep.
- For p1, defining abstraction (removing unneeded detail) instead of decomposition.
A full-marks answer
(a) Decomposition means breaking the whole booking problem into smaller sub-problems, each of which can be designed, written and tested on its own [p1]. For the Lumen these could be: choosing a showing of a film [p2], and taking the card payment [p3]. (Others are showing the seat plan and picking seats, and making and emailing the e-ticket.)
(b) The 0s and 1s leave out who has booked each seat [p4]. That helps because picking seats only needs to know whether each seat is free, so the data is small and the code that searches it is simpler to write and to check [p5].
Complete the trace table for this Java code. The Lumen cinema stores one row of seats as an array: 0 is a free seat and 1 is a booked seat. This code goes along the row from seat 0. A cell stays empty for any variable a line leaves unchanged. Then answer the question below the table. [4]
int[] row = {1, 0, 0, 1, 0};
int free = 0;
int first = -1;
for (int i = 0; i < row.length; i++) {
if (row[i] == 0) {
free = free + 1;
if (first == -1) {
first = i;
}
}
}
Why does first start at -1 and not at 0?
The table, made by running the code:
| i | free | first |
|---|---|---|
| 0 | ||
| -1 | ||
| 0 | ||
| 1 | ||
| 1 | ||
| 1 | ||
| 2 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 3 | ||
| 5 |
- p1 [1]
The
icolumn shows 0, 1, 2, 3, 4, then 5, the value that ends the loop. - p2 [1]
The
freecolumn shows 0 at the start, then 1 wheniis 1, 2 wheniis 2, and 3 wheniis 4, and does not change wheniis 0 or 3. - p3 [1]
The
firstcolumn shows -1 at the start and changes only once, to 1 wheniis 1. - p4 [1]
Says 0 is a real seat position, so starting at 0 would look like seat 0 was free; -1 cannot be a seat, so it means no free seat has been found yet.
The Lumen cinema's booking system stores one row of seats as a list (an array in Java) of whole numbers: 0 is a free seat and 1 is a booked seat. Seat positions start at 0. A group wants to sit together. Write a function that returns the position of the first seat of the first block of n free seats next to each other.
The design team wrote this plan for it:
RUN = 0
loop through the row from position 0
if the seat is free, add 1 to RUN
otherwise set RUN back to 0
if RUN equals n, return the position
of the first seat of this block
return -1
- A block is
nfree seats in positions next to each other. If there is more than one such block, return the position of the first seat of the one that comes first (the lowest position). - If the row has no block of
nfree seats, return-1. - A block may reach the last seat of the row.
nis a whole number from 1 to 30. The row holds only 0s and 1s, has at most 30 seats, and can be empty.
Only the number your function returns is checked.
def first_block(row: list[int], n: int) -> int
public static int firstBlock(int[] row, int n)
Marked by running your code against tests. Open it in the runner to check your answer. The hidden tests check:
- an empty row (rules 2 and 4)
- the whole row is the block, starting at seat 0 (rule 1)
- a block too short at the start, then a long enough one later (rule 1)
- four free seats together hold more than one block of 2, and the first starts at seat 1 (rule 1)
- one free seat, which is both the first and the last seat (rules 1 and 3)
- the only block of 2 is at the end of the row (rule 3)
- the group is bigger than the free seats there are (rule 2)
- a block at the start and a longer stretch later; the first block wins (rule 1)
[5]