Flashcards

1.4.2 Data structures

Card 1 of 22

  1. Question

    What is an array?

    Answer

    A static, ordered set of elements of the same data type under one name, each found by its index (starting at 0).

  2. Question

    How would you refer to one element of a 3D array?

    Answer

    With three indexes, such as readings[2,13,0]: for example day 2, hour 13, sensor 0.

  3. Question

    What is a record?

    Answer

    A group of named fields, which can be of different types, describing one item. A field is accessed with a dot, as in student.surname.

  4. Question

    How does a tuple differ from a list?

    Answer

    A tuple is immutable: once created, its items cannot be changed, added or removed. A list is mutable and dynamic.

  5. Question

    What is the difference between a static and a dynamic data structure?

    Answer

    A static structure (an array) has a fixed size set when it is created. A dynamic structure (a list or linked list) can grow and shrink while the program runs.

  6. Question

    What does each node of a linked list contain?

    Answer

    Its data and a pointer to the next node. A start pointer gives the first node and the last node's pointer is null.

  7. Question

    Describe how to add an item to an ordered linked list.

    Answer

    Store the item in a free node. Follow the pointers to find its place. Set the new node's pointer to the previous node's old pointer, then set the previous node's pointer to the new node.

  8. Question

    Describe how to remove an item from a linked list.

    Answer

    Follow the pointers to the node before it, set that node's pointer to the removed node's next pointer so it is skipped, and return the removed node to the free list.

  9. Question

    What is a stack, and which operations does it have?

    Answer

    A last in, first out (LIFO) structure. push adds to the top, pop removes and returns the top item, and peek returns it without removing it.

  10. Question

    What are stack overflow and underflow?

    Answer

    Overflow is pushing onto a full stack. Underflow is popping from an empty stack.

  11. Question

    What is a queue, and which operations does it have?

    Answer

    A first in, first out (FIFO) structure. enqueue adds an item at the rear, and dequeue removes and returns the item at the front.

  12. Question

    Why use a circular queue instead of a linear queue?

    Answer

    In a linear queue, space freed at the front is never reused. A circular queue wraps its pointers round to the start, so all the spaces in the array can be reused.

  13. Question

    How does the rear pointer of a circular queue move when an item is added?

    Answer

    rear = (rear + 1) MOD maxSize, so it wraps from the last position back to 0.

  14. Question

    Define root, leaf and subtree.

    Answer

    The root is the top node, with no parent. A leaf is a node with no children. A subtree is a node together with all the nodes below it.

  15. Question

    What rule does a binary search tree follow?

    Answer

    Each node has at most two children. Everything in a node's left subtree is smaller than it, and everything in its right subtree is larger.

  16. Question

    Give the orders for pre-order, in-order and post-order traversal.

    Answer

    Pre-order: node, left, right. In-order: left, node, right. Post-order: left, right, node. In-order visits a binary search tree's items in sorted order.

  17. Question

    How do you remove a node with two children from a binary search tree?

    Answer

    Replace its value with its in-order successor, the smallest value in its right subtree (or its in-order predecessor, the largest in its left subtree), then remove that node, which has at most one child.

  18. Question

    What is the difference between a directed and an undirected graph?

    Answer

    In a directed graph each edge goes one way only. In an undirected graph each edge can be followed both ways.

  19. Question

    When is an adjacency list better than an adjacency matrix?

    Answer

    For a sparse graph (few edges): the list only stores edges that exist, so it uses less memory. A matrix is quicker for checking whether a particular edge exists.

  20. Question

    Which structure does a depth-first traversal use, and which does a breadth-first traversal use?

    Answer

    Depth-first uses a stack (or recursion). Breadth-first uses a queue.

  21. Question

    What is a collision in a hash table, and how can linear probing deal with it?

    Answer

    A collision is when two keys hash to the same index. Linear probing stores the new item in the next free space, wrapping round to the start of the table if needed.

  22. Question

    How is an item removed from a hash table that uses linear probing?

    Answer

    Hash the key and probe until the item is found, then mark its space as deleted rather than empty, so later searches still probe past it.

Keys: Space flips, ← → move between cards.

Tap the card to flip it. Swipe to change card.