Exam code: 9618
1/2580Still learning
Know0
Define linear search.
A standard algorithm used to find elements in an unordered list, searching sequentially and systematically from the start to the end, one element at a time.

Join for free to unlock a full flashcard set, track what you know,
and turn revision into real progress.
In a linear search the list is searched and systematically from the start to the end.
In a linear search the list is searched sequentially and systematically from the start to the end.
Does a linear search need a sorted list?
No. It works on an unordered list.
Was this flashcard helpful?
Define linear search.
A standard algorithm used to find elements in an unordered list, searching sequentially and systematically from the start to the end, one element at a time.
In a linear search the list is searched and systematically from the start to the end.
In a linear search the list is searched sequentially and systematically from the start to the end.
Does a linear search need a sorted list?
No. It works on an unordered list.
True or False?
A linear search requires the list to be sorted.
False.
A linear search works on an unordered list.
What does a linear search do with each element?
It compares each element to the value being searched for.
What happens if the value is found?
The algorithm outputs where it was found in the list.
What happens if the value is not found?
It outputs a message stating that the value is not in the list.
Give an example use of a linear search.
Looking for a specific student name in a list, or searching for an item in a shopping list.
What is the worst case time complexity of a linear search?
O(n)
What is the best case time complexity of a linear search, and when does it occur?
O(1), when the item is found on the first comparison.
True or False?
The best case for a linear search is O(n).
False.
The best case is O(1), where the item is found on the first comparison. O(n) is the worst case.
What is the average case time complexity of a linear search?
O(n/2), which after removing coefficients is still O(n).
Why does the linear term dominate the constant term in Big-O?
Linear time dominates constant time. The constant term and coefficients contribute significantly less as the input size n grows larger.
What does the linearSearch function return?
The index of the item if it is found, or -1 if it is not.
If the item is not found, the linear search returns .
If the item is not found, the linear search returns -1.
In the linear search pseudocode, which three variables are initialised, and to what?
index to -1, i to 0, and found to FALSE.
Write the loop condition used in the linear search.
WHILE i < LENGTH(list) AND found = FALSE
What happens when a match is found?
index ← i saves the current index, and found ← TRUE stops the search.
Why is the found flag used?
To stop the search once the item has been found, rather than checking the rest of the list.
Define binary search.
A binary search compares the middle item to the target item, halving the search space with each step. It is more efficient than a linear search.
What must be true of the list for a binary search to work?
The list must be sorted.
True or False?
A binary search works on an unsorted list.
False.
The list must be sorted for a binary search to work correctly.
What happens if the middle value matches the target?
The index is returned.
What happens if the target is less than the middle value?
The top half is ignored.
What happens if the target is larger than the middle value?
The bottom half is ignored.
True or False?
A binary search deletes the half of the list it discards.
False.
It does not discard, delete or remove parts of the list. It only adjusts the start, end and mid pointers, which gives the appearance that items have been removed.
A binary search only adjusts the start, end and pointers.
A binary search only adjusts the start, end and mid pointers.
When does the binary search stop searching?
When the item is found, or when the item is shown not to be in the list.
What is the time complexity of a binary search?
O(log n), because it halves the search space with each iteration of the loop.
A binary search is an example of time complexity.
A binary search is an example of logarithmic time complexity.
Define divide and conquer.
An approach where a problem is progressively reduced in size, so that when each problem is at its smallest it is easiest to solve.
How many items remain after i iterations of a binary search?
n divided by 2 to the power i. The algorithm starts with n items, then n/2, then n/4, then n/8, and so on.
What is the best case time complexity of a binary search, and when?
O(1), where the item is found on the first comparison.
What is the average case time complexity of a binary search?
O(log n / 2), which after removing coefficients is still O(log n).
Why is the rounding of the midpoint flexible?
Rounding up or down are both valid, provided the same rounding is used consistently throughout the algorithm.
What does the binarySearch function return?
The index of the item if it is found, or -1 if it is not found.
In the binary search pseudocode, what are start and end initialised to?
start to 0, and end to LENGTH(list) - 1.
Write the loop condition used in the binary search.
WHILE start <= end AND found = FALSE
How is the middle index calculated in the binary search pseudocode?
mid ← (start + end) DIV 2
What happens if list[mid] is less than the item?
start ← mid + 1, which searches the right half.
What happens if list[mid] is greater than the item?
end ← mid - 1, which searches the left half.
When does the binary search loop end?
When the item is found, or when the search range becomes invalid.
Define bubble sort.
A bubble sort puts items into order smallest to largest, by comparing pairs of elements and swapping them if they are out of order.
Which pairs are compared during a pass?
The first to the second, the second to the third, and so on, until the second to last is compared to the last.
Define pass in a bubble sort.
A pass is one complete run of comparisons through the list.
What is true at the end of a pass?
The value at the top of the list is now in order, and the sort resets back to the start of the list to sort the next largest value.
Why does each pass get shorter?
Because after each pass the previously sorted final value is in order and does not need to be rechecked.
How does a bubble sort know it has finished?
A final pass checks all elements, and if no swaps are made the sort is complete.
A bubble sort is complete when a final pass makes swaps.
A bubble sort is complete when a final pass makes no swaps.
Give an example use of a bubble sort.
Sorting an array of names into alphabetical order, or sorting an array of student marks from a test.
What is the worst case time complexity of a bubble sort?
O(n squared)
What is the best case for a bubble sort, and when does it occur?
O(n), for an already sorted list, since each item must still be compared to check it is in order.
True or False?
The best case for a bubble sort is O(n squared).
False.
The best case is O(n) for an already sorted list. O(n squared) is the worst case.
What is the average case for a bubble sort?
O(n squared / 2) for an almost sorted list, which after removing coefficients is still O(n squared).
What is the space complexity of a bubble sort, and why?
O(1), because it operates in-place, requiring only a fixed amount of memory for loop counters and a temporary swap variable.
A bubble sort operates , requiring only a fixed amount of memory.
A bubble sort operates in-place, requiring only a fixed amount of memory.
True or False?
A bubble sort needs extra memory proportional to the size of the list.
False.
It has space complexity O(1), because it sorts in-place.
In the bubble sort pseudocode, what does last store?
The length of the list.
In the bubble sort pseudocode, what does i track?
The number of completed passes.
What is the swap variable used for?
It is a boolean flag used to check whether any swaps occurred during a pass.
Why is swap set to TRUE before the main loop begins?
To assume a swap will happen, so that the loop is entered at least once.
Write the main WHILE condition of the bubble sort.
WHILE i < (last - 1) AND swap = TRUE
What is swap set to at the start of each pass?
FALSE
What range does the inner FOR loop cover?
From index 0 to last - i - 2, which is the unsorted part of the list.
Write the three lines that perform the swap in a bubble sort.
temp ← list[j], then list[j] ← list[j + 1], then list[j + 1] ← temp
What happens after the inner loop completes?
i is incremented, which shortens the range checked on the next pass.
In an efficient bubble sort, what does reducing the Boundary each pass achieve?
It shortens the range checked on each subsequent pass, because the end of the list is already sorted.
Define insertion sort.
An insertion sort sorts one item at a time by placing it in the correct position of an unsorted list, repeating until all items are in the correct position.
The insertion sort places each item in the correct position of an list.
The insertion sort places each item in the correct position of an unsorted list.
What is the current item compared to?
Each previous item in the list.
What happens if the current item is smaller than the previous item?
The previous item is moved to the right and the current item takes its place.
What happens if the current item is larger than the previous item?
It is already in the correct position, and the next item is then sorted.
When does the insertion sort process stop?
When all items are in the correct position.
What is the worst case time complexity of an insertion sort?
O(n squared)
What is the best case for an insertion sort, and when?
O(n), for an already sorted list, where each item is checked one at a time.
What is the average case for an insertion sort?
O(n squared / 2) for a half sorted list, which after removing coefficients is still O(n squared).
What is the space complexity of an insertion sort, and why?
O(1), because the insertion sort requires no additional space.
True or False?
An insertion sort needs a second array to hold the sorted items.
False.
It requires no additional space, so its space complexity is O(1).
At which index does an insertion sort start, and why?
Index 1, not 0, because the first item is considered already sorted.
True or False?
An insertion sort starts at index 0.
False.
It starts at index 1, because the item at index 0 is considered already sorted.
In the insertion sort pseudocode, what does item store?
The current value to be positioned.
In the insertion sort pseudocode, what does position track?
Where item should go in the sorted portion of the list.
Write the outer FOR loop of the insertion sort.
FOR i ← 1 TO n - 1
Write the inner WHILE condition of the insertion sort.
WHILE position > 0 AND list[position - 1] > item
What does the inner WHILE loop do?
It shifts elements in the sorted portion to the right while they are greater than item.
In an insertion sort, elements greater than the item are to the right.
In an insertion sort, elements greater than the item are shifted to the right.
Name the two conditions that stop the inner loop.
The start of the list is reached (position = 0), or the correct spot is found (list[position - 1] <= item).
What happens after the inner loop finishes?
list[position] ← item inserts the value in its correct position.
An insertion sort runs on [5, 9, 4, 2, ...].
What is the list after the pass at i = 2?
[4, 5, 9, 2, ...]. The value 4 is shifted in front of both 9 and 5.
Name the six main stack operations.
isEmpty(), isFull(), push(value), pop(), peek() and size().
What three fields does a record-based stack have?
items, top and capacity.
In pseudocode, a stack is assumed to be -based, with items, top and capacity fields.
In pseudocode, a stack is assumed to be record-based, with items, top and capacity fields.
What is top set to for an empty stack?
-1
True or False?
A stack's top pointer is 0 when the stack is empty.
False.
It is -1 when empty, which is why size is calculated as top + 1.
Write the body of the isEmpty function for a stack.
RETURN s.top = -1
Write the body of the isFull function for a stack.
RETURN s.top = s.capacity - 1
What are the two steps of a push?
Increment top, then assign the value to s.items[s.top].
A push must check the stack is not before adding a value.
A push must check the stack is not full before adding a value.
What happens if you push onto a full stack?
It outputs "Stack is full".
What are the steps of a pop?
Read s.items[s.top] into a value, then decrement top, then return the value.
What does pop() return if the stack is empty?
-1
What does peek() do?
It returns s.items[s.top] without removing it, provided the stack is not empty.
True or False?
peek() removes the top value from the stack.
False.
peek() returns the top value without removing it. It is pop() that removes.
How is the size of a stack calculated?
RETURN s.top + 1
Why is the size top + 1?
Because top is a zero-based index, and it is -1 when the stack is empty.
10, 20 and 30 are pushed onto an empty stack.
What does peek() return?
30, the value at the top of the stack.
10, 20 and 30 are pushed, then two values are popped.
What is the size of the stack?
1. The pops return 30 then 20, leaving only 10.
In the PushAnimal() example, what is checked first?
Whether the stack is full, using IF AnimalTopPointer = 20.
What does PushAnimal() return if the stack is full?
FALSE
What three things does PushAnimal() do if there is room?
Inserts the value at Animal[AnimalTopPointer], increments the pointer, and returns TRUE.
Define queue as an ADT.
A queue is a linear ADT that follows the First In, First Out (FIFO) principle.
A queue is a ADT that follows the First In, First Out principle.
A queue is a linear ADT that follows the First In, First Out principle.
True or False?
A queue follows the Last In, First Out principle.
False.
A queue is First In, First Out (FIFO). It is a stack that is Last In, First Out.
Name the five main queue operations.
Enqueue, dequeue, peek or front, isEmpty and isFull.
What does enqueue do?
Adds an item to the back of the queue.
What does dequeue do?
Removes an item from the front of the queue.
What does peek do on a queue?
Looks at the front item without removing it.
Which fields does the circular queue record use?
items, front, rear, size and capacity.
Write the line that advances the rear pointer on enqueue.
q.rear ← (q.rear + 1) MOD q.capacity
Why is MOD used on the queue pointers?
So the pointer wraps around to the start of the array, which is what makes the queue circular.
The MOD operator makes the pointer around to the start of the array.
The MOD operator makes the pointer wrap around to the start of the array.
What three things does enqueue do when the queue is not full?
Advances rear, stores the item at q.items[q.rear], and increments size.
What does enqueue output if the queue is full?
"Queue is full"
What does dequeue do when the queue is not empty?
Reads q.items[q.front] into a value, advances front using MOD, decrements size, and returns the value.
True or False?
In a circular queue, dequeue physically shifts every remaining item forward.
False.
It only advances the front pointer, using MOD to wrap around the array.
What does dequeue return if the queue is empty?
"Queue is empty"
Write the body of the isEmpty function for a queue.
RETURN q.size = 0
Write the body of the isFull function for a queue.
RETURN q.size = q.capacity
How does the find function search a circular queue?
It performs a linear search, starting at q.front and stepping forward with pos ← (pos + 1) MOD q.capacity for q.size items.
What does the find function return?
TRUE if the target is found, otherwise FALSE.
Why does find loop only q.size times rather than q.capacity?
Because only size items are actually in the queue, even though the array has capacity slots.
Name the four operations you must be able to program on a linked list.
Create a linked list, traverse it, add data to it, and remove data from it.
What two fields does a linked list Node class have?
A data field, for example fruit, and a next field storing a reference to the next node in the list.
In a linked list, the next field stores a to the next node.
In a linked list, the next field stores a reference to the next node.
How is a connection between nodes established?
By setting the next field of each node to the appropriate next node.
What is the next field of the last node set to?
NULL
How do you traverse a linked list?
Start at the head, output the current node's value, then set current to current.next, repeating until current is NULL.
Write the traversal loop condition for a linked list.
WHILE current != NULL
Traversal of a linked list stops when current becomes .
Traversal of a linked list stops when current becomes NULL.
What is the first step in adding a node to the end of a linked list?
Create a new node with the new data, and set its next field to NULL.
What is checked before appending a node?
Whether the list is empty, that is IF node1 = NULL.
What happens if the list is empty when adding a node?
The new node becomes the head of the list.
How do you find the end of a linked list?
Traverse with WHILE current.next != NULL, then set current.next to the new node.
Which two pointers does the removal algorithm track?
current and previous.
What happens when the node to remove is at the head?
previous is NULL, so the head is updated to current.next.
True or False?
The head of a linked list never changes.
False.
If the node being removed is at the head, the head is updated to current.next.
What happens when the node to remove is not at the head?
previous.next is set to current.next, which bypasses the node.
True or False?
Removing a node from a linked list requires shifting the remaining nodes.
False.
It only updates the previous node's pointer to bypass the removed node.
What happens immediately after a node is removed?
The loop breaks, so no further nodes are checked.
How do the pointers advance if no match is found?
previous ← current, then current ← current.next.
A node containing B is inserted into a linked list in alphabetic order.
What is the first step?
Check for a free node.
A node containing B is inserted in alphabetic order.
What is the second step?
Search for the correct insertion point.
When inserting a new value, where is the data value assigned?
To the first node in the free list, that is the node pointed to by the free list's start pointer.
B is inserted between A and C.
Which two pointers change?
The pointer from A is changed to point to B instead of C, and the pointer from B points to C.
What happens to the free list after an insertion?
Its start pointer moves to point to the next free node.
Define binary tree.
A binary tree is a rooted tree where every node has a maximum of 2 nodes.
True or False?
A node in a binary tree can have three children.
False.
Every node in a binary tree has a maximum of 2.
What is a binary tree essentially?
A graph, and it can therefore be implemented in the same way.
What is the most common way to represent a binary tree?
By storing each node with a left and right pointer.
Name the three things you must be able to do with a tree.
Traverse the tree data structure, add new data to a tree, and remove data from a tree.
Define node.
An item in a tree.
Define edge.
An edge connects two nodes together, and is also known as a branch or pointer.
Define root.
A single node which does not have any incoming nodes.
A root is a single node which does not have any nodes.
A root is a single node which does not have any incoming nodes.
Define child.
A node with incoming edges.
Define parent.
A node with outgoing edges.
What is the difference between a parent and a child, in terms of edges?
A parent has outgoing edges. A child has incoming edges.
Define leaf.
A node with no children.
True or False?
A leaf node has outgoing edges.
False.
A leaf is a node with no children, so it has no outgoing edges.
Define subtree.
A subsection of a tree, consisting of a parent and all the children of that parent.
Define traversing.
The process of visiting each node in a tree data structure, exactly once.
Traversing is the process of visiting each node in a tree once.
Traversing is the process of visiting each node in a tree exactly once.
What does a pre-order traversal output first?
The value of the current node, before visiting any of its children.
What is the base case of a recursive tree traversal?
IF node = NULL THEN RETURN
When removing a child by value, what does the procedure do?
It searches the parent's children for a matching value, removes the first match, and then returns.
What is the limitation of that removal procedure?
It only removes the first matching child from the direct children of a node. Removing nodes anywhere in the tree would need a recursive version.
What does 'ADT from ADT' mean?
Being able to explain or demonstrate how one abstract data type can be built on top of another.
Give three examples of an ADT built from another ADT.
A stack from a linked list, a queue from two stacks, and a dictionary from a binary search tree.
True or False?
An ADT can only be implemented using built-in types.
False.
You are not limited to built-in types. ADTs can be composed out of each other.
Which built-in type implements a stack?
A list or array, using push and pop operations.
Which built-in types implement a queue?
A list or array, or a circular buffer.
How is a linked list implemented from built-in types?
As a class with next fields, implementing the nodes manually.
Which built-in type implements a dictionary?
A hash table, giving key-value mapping.
How is a binary tree implemented from built-in types?
As a class with left and right fields, where each node links to its children.
How can a stack be built from a linked list?
Push and pop from the head of the list.
How can a dictionary be built from a binary search tree?
Store the keys and values in the BST, which gives ordered lookup.
How can a binary tree be built from another ADT?
From a linked list or array, where the nodes link to each other like a graph.
A queue can be implemented using two .
A queue can be implemented using two stacks.
What are the two stacks called in a two-stack queue?
inStack and outStack.
How does enqueue work in a two-stack queue?
Push the item onto inStack.
How does dequeue work in a two-stack queue?
If outStack is empty, pop all items from inStack and push them onto outStack, then pop from outStack.
True or False?
In a two-stack queue, dequeue always pops directly from inStack.
False.
If outStack is empty, all items are first moved from inStack to outStack, and the pop then happens from outStack.
What does the two-stack queue demonstrate?
That you can simulate FIFO behaviour using LIFO operations.
Two stacks can simulate behaviour using LIFO operations.
Two stacks can simulate FIFO behaviour using LIFO operations.
Why must algorithms be compared?
To determine how suitable they are for solving a specific problem or working with a particular set of data.
Name the two key factors in comparing algorithms.
Time complexity and space complexity.
What makes an algorithm most suitable?
It solves the problem in the shortest time and using the least memory.
Define time complexity.
The number of operations or steps an algorithm takes to complete, not the actual time in seconds or minutes.
True or False?
Time complexity measures the time in seconds that an algorithm takes.
False.
It measures the number of operations or steps, not the actual time.
Why is time complexity independent of hardware?
A faster CPU executes instructions more quickly, but the number of instructions the algorithm performs stays the same.
How does the marathon analogy explain time complexity?
The marathon distance is the algorithm and the runners are different CPUs. Each runner finishes at a different time, but the distance is the same.
Define space complexity.
The amount of memory an algorithm needs to complete its task.
Name three things that memory usage includes.
Space for input data, temporary variables, and any additional data structures such as arrays, stacks or queues.
Why might a recursive algorithm use more memory?
Because of the call stack.
Define Big O Notation.
A mathematical way to describe the time and space complexity of an algorithm, showing how well it scales as the size of the input increases.
What does Big O describe?
The order of growth, or efficiency, of an algorithm.
Big O is -independent, measuring steps rather than seconds.
Big O is hardware-independent, measuring steps rather than seconds.
True or False?
A faster CPU improves an algorithm's Big O complexity.
False.
Big O is hardware-independent. It measures steps or operations, not seconds.
State the two Big O rules.
Keep only the dominant term, and ignore constants.
Only the factor is used when determining an algorithm's Big O.
Only the dominant factor is used when determining an algorithm's Big O.
Why are constants ignored in Big O?
They become insignificant as the input size grows.
What is O(1)?
Constant time. The algorithm always takes the same number of steps, regardless of input size.
What is O(n)?
Linear time. The number of steps increases proportionally with the input size.
What is O(n squared)?
Polynomial time. Performance is proportional to the square of the input size, caused by nested loops.
What is O(log n)?
Logarithmic time. The number of steps grows slowly even as the input size grows rapidly. Common in binary search.
What is O(2 to the power n)?
Exponential time. The number of steps doubles for every additional input value, so it grows extremely fast.
What is O(n log n)?
Linear logarithmic time, for algorithms that divide data and process each part. Merge sort is an example.
Order the complexities from most to least inefficient.
Exponential, then polynomial, then linear, then logarithmic, then constant.
Where does factorial time sit?
It performs worse than exponential time.
Where does linear-logarithmic time sit?
Between polynomial and linear time.
Simplify 3n squared + 4n + 5 to Big O.
O(n squared), because n squared is the dominant factor and coefficients are removed.
Why is the coefficient 3 dropped from 3x squared?
As the input grows arbitrarily large, multiplying by 3 contributes very little to the overall time complexity.
How do you derive Big O by counting loops?
1 loop is O(n), 2 nested loops are O(n squared), and no loops is O(1).
What is log base 2 of 1,024?
10, because 2 to the power 10 is 1,024.
Which case does Big O usually describe, and why?
The worst case, in order to guarantee a minimum standard of performance.
For a linear search, what are the best, average and worst cases?
Best O(1) when the item is first, average O(n) when it is in the middle, and worst O(n) when it is not found.
Define binary tree.
A binary tree is a rooted data structure where each node can have a maximum of two child nodes, one on the left and one on the right.
Each node in a binary tree can have a maximum of child nodes.
Each node in a binary tree can have a maximum of two child nodes.
What are binary trees used for?
Storing data in a way that allows fast searching, insertion and deletion.
How are the nodes of a binary tree linked?
Using pointers that reference the left and right child nodes.
What does a binary tree node contain?
A data value, plus Left and Right pointers, which may be NULL if no child exists.
Define root.
The topmost node, which has no parent.
Define leaf.
A node with no children.
Define edge in a tree.
A connection between two nodes, also called a branch or pointer.
Define subtree.
A smaller tree that is part of a larger one.
Define traversal.
The process of visiting every node exactly once.
What is the difference between a parent and a child?
A parent links to one or more children. A child is a node linked from a parent.
Define binary search tree (BST).
A type of binary tree that stores data in sorted order.
State the two rules of a binary search tree.
All values in the left subtree are less than the node's value. All values in the right subtree are greater than or equal to the node's value.
True or False?
In a BST, values equal to the node go in the left subtree.
False.
Values greater than or equal to the node go in the right subtree.
Why are binary search trees efficient?
They use comparison to decide which branch to follow, which makes searching and insertion efficient.
What three fields does a TreeNode class have?
A Value, a Left pointer and a Right pointer.
What does createNode do?
It creates a new node, sets its Value to the data passed in, and sets both Left and Right to NULL.
How do you insert a value into a binary search tree?
Compare it with the current node. If smaller, move left; if greater or equal, move right. Insert the new node when an empty pointer is found.
Name the three traversal methods.
Pre-order, in-order and post-order.
What is the pre-order sequence?
Visit the current node, then left, then right.
What is the in-order sequence?
Visit left, then the current node, then right.
What is the post-order sequence?
Visit left, then right, then the current node.
A tree has root 10, children 5 and 15, and 15 has children 12 and 20.
What is the pre-order output?
10 5 15 12 20
A tree has root 10, children 5 and 15, and 15 has children 12 and 20.
What is the in-order output?
5 10 12 15 20
A tree has root 10, children 5 and 15, and 15 has children 12 and 20.
What is the post-order output?
5 12 20 15 10
True or False?
The base case of a recursive traversal is reaching a leaf node.
False.
The base case is IF Node = NULL THEN RETURN, that is reaching a NULL pointer, not a leaf.
Name the three cases for deleting a node from a BST.
The node has no children, one child, or two children.
How do you delete a node with no children?
Simply remove the node.
How do you delete a node with one child?
Replace the node with its single child.
How do you delete a node with two children?
Replace the node's value with its in-order successor, the smallest value in the right subtree, then delete that successor node.
To delete a node with two children, replace it with its in-order .
To delete a node with two children, replace it with its in-order successor.
What does the findMin helper function do?
It walks left from a node until Left is NULL, returning the smallest value in that subtree.
By signing up you agree to our Terms and Privacy Policy