60 practice questions
How do you access element at row 2, column 3 of a 2D array?
easyA program stores a student's name, age and exam scores together as a single unit. Which data structure is this an example of?
easyExplain why a 2D array might be used to represent a chessboard, and how a specific square would be accessed.
mediumWhich of the following is NOT typically considered a primitive data type?
easyExplain how a negative integer is typically represented in binary using two's complement.
mediumA character (e.g. a single letter) is stored in a computer as:
easyExplain how a Boolean data type is stored, and why this is not always the most efficient use of memory.
mediumReal (floating-point) numbers are typically stored in a computer using:
mediumExplain the key difference between how an integer and a real (floating-point) number are stored in binary, and why this matters for a programmer.
mediumWhat is the range of values that can be represented by an unsigned 8-bit binary number?
easyWhat data structure uses LIFO ordering?
easyQueue [A,B,C]. Dequeue once, enqueue D. Result?
mediumWhat is a stack overflow in the context of recursion?
mediumWhat does a priority queue do differently from a regular queue?
mediumA stack is implemented using an array. What operation adds an item?
easyExplain what is meant by an abstract data type (ADT).
mediumWhich data structure operates on a First-In-First-Out (FIFO) basis?
easyDescribe how a stack could be used to check whether a string of brackets, e.g. "({[]})", is correctly balanced.
hardA stack is empty. The following operations are performed in order: push(5), push(8), peek(), pop(), push(3), pop(). State the value returned by each peek() and pop() operation, and the final contents of the stack.
mediumWhat is the main purpose of a 'circular queue' (compared to a standard linear queue implemented with an array)?
mediumExplain static vs dynamic data structures with one advantage each.
mediumWhat is the difference between a singly and doubly linked list?
mediumDescribe post-order traversal of a binary tree.
mediumGive one advantage each of adjacency matrix vs adjacency list for graphs.
hardWhat is the difference between a binary tree and a binary search tree?
mediumExplain why a linked list can be more efficient than an array for inserting an item in the middle of a large collection.
mediumExplain the difference between a static data structure and a dynamic data structure, giving an example of each.
mediumA binary search tree is used to store integers. Which traversal would visit the nodes in ascending numerical order?
mediumExplain why choosing an appropriate data structure is important when designing a program, using a real-world example.
mediumDescribe a real-world use case for a graph data structure, explaining what the nodes and edges represent.
mediumWhat is a hash table and what is a collision?
mediumWhat is the worst-case time complexity of searching for an item in a hash table?
hardWhat is a 'hash function' used for in a hash table?
easyExplain how 'linear probing' resolves a collision in a hash table.
mediumIn a hash table that uses 'chaining' to resolve collisions, each array index stores:
mediumExplain why it is important for a hash function to distribute keys as evenly as possible across the available indices.
mediumThe 'load factor' of a hash table is best described as:
mediumExplain the trade-off involved in choosing the size of the underlying array for a hash table.
mediumCompared to searching an unsorted linked list of n items for a particular value, searching a well-implemented hash table for the same value is generally:
hardExplain what is meant by 'rehashing' a hash table, and when it might be triggered.
hardWhat is the difference between depth-first and breadth-first graph traversal?
hardA graph has the following adjacency information: A connects to B and C; B connects to A and D; C connects to A; D connects to B. Starting at A, list the order in which nodes are visited using breadth-first search (BFS).
mediumBreadth-first search (BFS) typically uses which data structure to keep track of which nodes to visit next?
easyUsing the same graph as before (A connects to B and C; B connects to A and D; C connects to A; D connects to B), list one possible order in which nodes are visited using depth-first search (DFS) starting at A.
mediumDepth-first search (DFS) typically uses which data structure (or technique) to keep track of which nodes to visit next?
easyDescribe, in outline, how Dijkstra's algorithm finds the shortest path from a start node to all other nodes in a weighted graph.
hardDijkstra's algorithm, as commonly taught, requires that all edge weights in the graph are:
mediumA graph has nodes A, B and C. The edge A-B has weight 4, the edge A-C has weight 1, and the edge C-B has weight 1. Using Dijkstra's algorithm starting at A, find the shortest distance from A to B, showing your reasoning.
hardBreadth-first search (BFS) is often used to find the shortest path between two nodes in:
mediumExplain a situation where breadth-first search (BFS) would be a more appropriate choice than depth-first search (DFS), and vice versa.
mediumWhat is the time complexity of searching a balanced BST?
mediumWhat is the worst-case time complexity of bubble sort?
mediumDescribe how merge sort works and state its time complexity.
hardWhich sorting algorithm repeatedly selects a pivot value and partitions the list into elements smaller and larger than the pivot?
mediumWhich of the following operations on an array typically has a time complexity of O(1)?
easyExplain the difference between O(n) and O(nยฒ) time complexity, giving an example algorithm for each.
mediumWhat is the time complexity of binary search on a sorted array of n elements?
mediumExplain why an algorithm with O(log n) time complexity is considered very efficient, even for large values of n.
mediumWhich of the following lists time complexities in order from most efficient (fastest growth) to least efficient (slowest/worst growth) for large n?
mediumExplain what Big O notation describes about an algorithm, and why two algorithms with the same Big O complexity might still perform differently in practice.
hardRepresent -45: +45 = 0010 1101 invert = 1101 0010 add 1 = 1101 0011 โ -45 Check place values (MSB is NEGATIVE): 1101 0011 = -128 +64 +16 +2 +1 = -45 โ 8-bit range: -128 (1000 0000) to +127 (0111 1111)
Add 0100 1111 (79) + 0011 0001 (49) = 1000 0000 (-128 in 2's complement!) The true answer 128 does not fit in 8 bits, so the sign bit flips โ OVERFLOW. Detect: adding two positives gives a negative (or two negatives give a positive).
Number = mantissa x 2^exponent, both in two's complement. NORMALISED form: mantissa starts 0.1โฆ (positive) or 1.0โฆ (negative) to maximise precision. Mantissa 0.1011, exponent 0011 (=3): move binary point right 3 โ 0101.1 = 5.5 Trade-off: more mantissa bits = more PRECISION; more exponent bits = greater RANGE.
Hex = binary in groups of 4 bits: 1011 0110 = B6 Logical left shift by 1 = x2: 0001 0110 (22) << 1 = 0010 1100 (44) Right shift by 1 = รท2 (integer): 0010 1100 (44) >> 1 = 0001 0110 (22) AND mask to test/clear bits: 1011 0110 AND 0000 1111 = 0000 0110 (low nibble)
push(item):
IF top = maxSize - 1 THEN
REPORT "stack overflow"
ELSE
top โ top + 1
stack[top] โ item
pop():
IF top = -1 THEN
REPORT "stack underflow"
ELSE
item โ stack[top]
top โ top - 1
RETURN item
Uses: the call stack, undo, backtracking, evaluating
expressions, converting infix โ postfix.enqueue(item): IF count = size THEN REPORT "queue full" rear โ (rear + 1) MOD size queue[rear] โ item ; count โ count + 1 dequeue(): IF count = 0 THEN REPORT "queue empty" item โ queue[front] front โ (front + 1) MOD size ; count โ count - 1 RETURN item MOD wraps the pointers so freed slots are reused โ otherwise the queue "walks off" the end of the array.
Tree: 8
/ \
3 10
/ \ \
1 6 14
In-order (L,Root,R): 1 3 6 8 10 14 โ SORTED order
Pre-order (Root,L,R): 8 3 1 6 10 14 โ copy/serialise
Post-order(L,R,Root): 1 6 3 14 10 8 โ delete/evaluate| Gate | Notation | Output is 1 whenโฆ |
|---|---|---|
| AND | A.B | both inputs are 1 |
| OR | A+B | at least one input is 1 |
| NOT | NOT A | the input is 0 |
| XOR | A โ B | the inputs are different |
| NAND | NOT(A.B) | NOT both inputs are 1 |
| NOR | NOT(A+B) | both inputs are 0 |
| Law | Rule |
|---|---|
| Commutative | A.B = B.A ; A+B = B+A |
| Associative | (A.B).C = A.(B.C) |
| Distributive | A.(B+C) = A.B + A.C |
| Absorption | A + A.B = A ; A.(A+B) = A |
| Identity | A.1 = A ; A+0 = A |
| Null | A.0 = 0 ; A+1 = 1 |
| Complement | A.(NOT A) = 0 ; A+(NOT A) = 1 |
| Double negation | NOT(NOT A) = A |
NOT(A AND B) = (NOT A) OR (NOT B) NOT(A OR B) = (NOT A) AND (NOT B) Method: "break the bar and swap the operator." Use them to push NOTs inward and to convert between AND/OR forms (useful for NAND/NOR circuits).
Simplify: A.B + A.(NOT B) + (NOT A).B = A.(B + NOT B) + (NOT A).B [distributive] = A.(1) + (NOT A).B [complement] = A + (NOT A).B [identity] = A + B [absorption variant] Final: A + B