1. Home
  2. Explore
  3. Computer Science
  4. Data Structures Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Data Structures Course

  • 68Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Data Structures Basics and Arrays

What are data structures and what is their purpose?

68 questions

  1. 1

    What are data structures and what is their purpose?

    Model answer

    Data structures are ways to store, organize, and manipulate data in a program.

    Explanation

    Data structures provide efficient operations for different use cases, such as faster access or modification based on the structure's properties.

  2. 2

    Explain the difference between data types and data structures.

    Model answer

    Data types are primitive pieces of data like integers, booleans, and strings. Data structures store and organize these data types (e.g., an array storing numbers).

    Explanation

    Data types are the basic building blocks; data structures combine and organize them to enable more complex operations.

  3. 3

    What are the two main categories of data structures?

    Model answer

    Linear and non-linear data structures.

    Explanation

    Linear structures store elements in a sequential order (e.g., arrays, linked lists). Non-linear structures do not store in a straight line (e.g., trees, graphs).

  4. 4

    Name four linear data structures.

    Model answer

    Array, linked list, stack, queue.

    Explanation

    These are linear because elements are arranged in a sequence, one after another.

  5. 5

    Name two non-linear data structures.

    Model answer

    Trees and graphs.

    Explanation

    Non-linear structures allow hierarchical or network-like connections, not a simple linear order.

  6. 6

    What is a fixed-size array and how does it differ from a dynamic array?

    Model answer

    A fixed-size array has a predetermined size that cannot change. A dynamic array can grow or shrink as elements are added or removed.

    Explanation

    Fixed arrays allocate a set number of slots at creation; dynamic arrays typically resize by creating a new larger array and copying elements.

  7. 7

    What is the time complexity of initializing a fixed-size array of size n?

    Model answer

    O(n) time.

    Explanation

    Each of the n memory slots must be allocated and initialized (e.g., set to zero or undefined), requiring n operations.

  8. 8

    Why is accessing an element by index in an array O(1) time?

    Model answer

    Because array elements are stored in contiguous memory; the address of any element is computed as base address + index Ɨ element size, allowing direct access.

    Explanation

    No traversal is needed; the memory location is calculated instantly from the index.

  9. 9

    What is the time complexity of inserting an element at an arbitrary position (not the end) in a fixed-size array? Why?

    Model answer

    O(n) because all elements after the insertion point must be shifted to the right to make space.

    Explanation

    If inserting at the beginning, every element must move; if near the end, fewer shifts, but worst-case is still O(n).

  10. 10

    What is the time complexity of deleting an element from the beginning of a fixed array?

    Model answer

    O(n) because all remaining elements must be shifted left to fill the gap.

    Explanation

    Shifting takes linear time proportional to the number of elements after the deletion point.

  11. 11

    When can insertion and deletion in an array be O(1) time?

    Model answer

    When inserting or deleting at the end of the array.

    Explanation

    No shifting is needed; you simply set the value at the next free index or decrement the length.

  12. 12

    How does a dynamic array typically handle resizing when it runs out of space?

    Model answer

    It creates a new fixed-size array of larger capacity (often double), copies all elements over, and then adds the new element.

    Explanation

    This resize operation takes O(n) time, but it happens infrequently, making appends amortized O(1).

  13. 13

    What is the amortized time complexity of appending an element to a dynamic array?

    Model answer

    O(1) amortized.

    Explanation

    Although occasional resizes cost O(n), they are rare enough that the average time per append is constant.

  14. 14

    What are the core components of a node in a singly linked list?

    Model answer

    A value (data) and a next pointer that references the next node in the list.

    Explanation

    The next pointer links nodes together; the last node's next is null.

  15. 15

    What is the significance of the head pointer in a linked list?

    Model answer

    The head points to the first node of the list; it provides the starting point to access all other nodes.

    Explanation

    Without the head, you cannot traverse or access any elements in the linked list.

  16. 16

    What is the time complexity of accessing an arbitrary element in a singly linked list?

    Model answer

    O(n) because you must traverse from the head until you reach the desired node.

    Explanation

    There is no direct index access; you follow next pointers sequentially.

  17. 17

    In a naive singly linked list (without tail pointer), what is the time complexity of inserting a node at the end?

    Model answer

    O(n) because you need to traverse the entire list to find the last node.

    Explanation

    Once at the last node, you set its next to the new node, but traversal itself takes linear time.

  18. 18

    How does a circular singly linked list improve insertion and deletion at the ends?

    Model answer

    By storing a pointer to the tail node, both the front and back can be accessed in O(1) time, allowing O(1) insert/delete at either end.

    Explanation

    The tail's next points to the head, so the head is reachable from the tail directly.

  19. 19

    What extra pointer does a doubly linked list node have compared to a singly linked list?

    Model answer

    A prev pointer that points to the previous node in the list.

    Explanation

    This allows traversal in both directions and can simplify certain operations like deletion when you already have a reference to the node.

  20. 20

    In a doubly linked list, what is the time complexity of inserting a node after a given node (when you have a reference to that node)?

    Model answer

    O(1) because you only need to update the pointers of the given node, the new node, and the following node.

    Explanation

    With direct access to the node, no traversal is needed; the operation involves a fixed number of pointer assignments.

  21. 21

    What is a disadvantage of a doubly linked list compared to a singly linked list?

    Model answer

    It requires more memory because each node stores an additional pointer (prev).

    Explanation

    The extra pointer increases space overhead, which can be significant for large lists.

  22. 22

    Define the FIFO property of a queue.

    Model answer

    First In, First Out: the first element added to the queue is the first one to be removed.

    Explanation

    This is analogous to a line of people; the first person in line is served first.

  23. 23

    List the four basic operations of a queue and their required time complexities.

    Model answer

    Enqueue (add to back): O(1), dequeue (remove from front): O(1), peek/front (view front): O(1), isEmpty: O(1).

    Explanation

    These operations must be efficient to make the queue practical.

  24. 24

    Why is implementing a queue with an array inefficient for either enqueue or dequeue?

    Model answer

    Because if the front of the queue is the front of the array, dequeue causes O(n) shifting; if the front is the back of the array, enqueue causes O(n) shifting.

    Explanation

    An array provides O(1) operations only at one end; the queue requires O(1) at both ends (add at back, remove from front).

  25. 25

    Which data structure is ideal for implementing a queue with O(1) enqueue and dequeue?

    Model answer

    A singly linked list with a pointer to the tail node.

    Explanation

    Enqueue adds at the tail (O(1) using tail pointer), dequeue removes from the head (O(1) using head pointer).

  26. 26

    Define the LIFO property of a stack.

    Model answer

    Last In, First Out: the most recently added element is the first one to be removed.

    Explanation

    Like a stack of plates: the last plate placed on top is the first one taken off.

  27. 27

    Why can an array efficiently implement a stack?

    Model answer

    Because stack operations (push and pop) occur at the same end (the top), and arrays support O(1) insertion and deletion at the end.

    Explanation

    No shifting is needed when adding or removing from the end of an array.

  28. 28

    Describe how two stacks can be used to implement a queue, and what is the consequence?

    Model answer

    Use an input stack and an output stack. For enqueue, push onto input stack; for dequeue, if output stack is empty, pop all from input and push onto output, then pop from output. This makes either enqueue or dequeue O(n) in the worst case.

    Explanation

    One of the operations becomes linear because elements must be transferred between stacks to reverse order.

  29. 29

    What is a binary tree?

    Model answer

    A tree data structure in which each node has at most two children (left and right).

    Explanation

    Binary trees are the foundation for more specialized trees like binary search trees and heaps.

  30. 30

    Define the terms root, leaf node, edge, and height in a binary tree.

    Model answer

    Root: the topmost node. Leaf node: a node with no children. Edge: the connection between two nodes. Height: the maximum number of edges from the root to a leaf.

    Explanation

    These terms describe the structure and relationships within a tree.

  31. 31

    What is the depth of a node in a binary tree?

    Model answer

    The number of edges from the node to the root.

    Explanation

    Depth, also called level, measures how far down the node is from the root.

  32. 32

    Describe a full binary tree.

    Model answer

    A binary tree in which every node has either 0 or 2 children (no node has exactly one child).

    Explanation

    Only leaf nodes have no children; internal nodes always have two children.

  33. 33

    Describe a complete binary tree.

    Model answer

    A binary tree where all levels except possibly the last are completely filled, and the last level has all nodes as far left as possible.

    Explanation

    This property allows efficient array representation for heaps.

  34. 34

    Describe a perfect binary tree.

    Model answer

    A binary tree where all internal nodes have two children and all leaf nodes are at the same depth (all levels are completely filled).

    Explanation

    A perfect binary tree of height h has exactly 2h+1āˆ’12^{h+1} - 12h+1āˆ’1 nodes.

  35. 35

    What is a balanced binary tree?

    Model answer

    A binary tree where for every node, the height of its left and right subtrees differ by at most 1.

    Explanation

    Balanced trees ensure O(log n) height, leading to efficient operations.

  36. 36

    What is the maximum number of nodes in a binary tree of height h?

    Model answer

    2h+1āˆ’12^{h+1} - 12h+1āˆ’1.

    Explanation

    This occurs when the tree is perfect (full and complete).

  37. 37

    What is the maximum possible height of a binary tree with n nodes?

    Model answer

    nāˆ’1n - 1nāˆ’1 (when the tree degenerates into a linked list).

    Explanation

    In a skewed tree, each node has at most one child, forming a linear chain.

  38. 38

    Explain the breadth-first search (BFS) traversal of a binary tree.

    Model answer

    BFS uses a queue: start with root, repeatedly dequeue a node, process it, and enqueue its left and right children. Nodes are visited level by level.

    Explanation

    This is also known as level-order traversal.

  39. 39

    Explain the depth-first search (DFS) traversal of a binary tree using a stack.

    Model answer

    DFS uses a stack: push root, then repeatedly pop a node, process it, and push its right then left child (or left then right, depending on desired order). Go as deep as possible along each branch.

    Explanation

    DFS can be implemented recursively; the iterative version uses a stack to simulate recursion.

  40. 40

    Describe the in-order traversal of a binary tree.

    Model answer

    Visit left subtree, then the current node, then right subtree.

    Explanation

    For a binary search tree, in-order traversal yields nodes in ascending order.

  41. 41

    Describe the pre-order traversal of a binary tree.

    Model answer

    Visit the current node first, then left subtree, then right subtree.

    Explanation

    Useful for copying the tree or getting a prefix expression.

  42. 42

    Describe the post-order traversal of a binary tree.

    Model answer

    Visit left subtree, then right subtree, then the current node.

    Explanation

    Useful for deleting the tree or evaluating postfix expressions.

  43. 43

    What is the time complexity of inserting a node into a general binary tree (not BST) using BFS?

    Model answer

    O(n) in the worst case because you may need to traverse all nodes to find the first available empty slot.

    Explanation

    BFS level-order traversal examines nodes until it finds a node missing a left or right child.

  44. 44

    Explain the general algorithm for deleting a node from a binary tree.

    Model answer

    Find the deepest rightmost leaf node (using DFS or BFS), copy its data to the node to be deleted, then delete that leaf node by removing its parent's reference.

    Explanation

    This ensures the tree remains connected and the deletion is straightforward.

  45. 45

    State the property of a binary search tree (BST).

    Model answer

    For every node, all values in its left subtree are less than the node's value, and all values in its right subtree are greater. Duplicates are placed consistently (all left or all right).

    Explanation

    This ordering enables efficient searching.

  46. 46

    What is the time complexity of searching for a value in a BST?

    Model answer

    O(h), where h is the height of the tree.

    Explanation

    At each step, you go left or right, discarding half the remaining tree, but if the tree is skewed, h can be O(n).

  47. 47

    Describe the algorithm for inserting a value into a BST.

    Model answer

    Start at root; compare value with current node; if less, go left; if greater, go right. Repeat until null is reached, then insert the new node there.

    Explanation

    This maintains the BST property and takes O(h) time.

  48. 48

    List the three cases for deleting a node from a BST and how each is handled.

    Model answer

    Case 1 (leaf): simply remove it. Case 2 (one child): bypass the node by linking its parent to its child. Case 3 (two children): replace the node's value with its inorder successor (or predecessor), then delete that successor (which falls into case 1 or 2).

    Explanation

    The inorder successor is the smallest value in the right subtree, found by going right once then left repeatedly.

  49. 49

    What is the worst-case time complexity of BST operations and when does it occur?

    Model answer

    O(n) occurs when the tree is skewed (e.g., inserting sorted values).

    Explanation

    A skewed BST degenerates into a linked list, making height equal to number of nodes.

  50. 50

    What is a max heap?

    Model answer

    A complete binary tree where each parent node has a value greater than or equal to its children (the root is the maximum element).

    Explanation

    This property ensures efficient access to the maximum.

  51. 51

    How is a heap typically represented in memory, and what are the formulas to find children and parent?

    Model answer

    Using an array with the root at index 0. For node at index i: left child at 2i+12i+12i+1, right child at 2i+22i+22i+2, parent at lfloor(iāˆ’1)/2floorlfloor (i-1)/2 floorlfloor(iāˆ’1)/2floor.

    Explanation

    This mapping works because the heap is a complete binary tree stored in level order.

  52. 52

    Describe the process of inserting a new element into a max heap.

    Model answer

    Add the element at the end of the array (next available position), then sift up: repeatedly swap with parent if the new element is larger, until the heap property is restored.

    Explanation

    Sifting up takes O(log n) time because the height of the tree is log n.

  53. 53

    Describe the process of extracting the maximum element from a max heap.

    Model answer

    Swap the root with the last element, remove the last element (which was the former root), then sift down the new root: repeatedly swap with the larger child until the heap property holds.

    Explanation

    This also takes O(log n) time.

  54. 54

    What is the time complexity of building a heap by inserting n elements one by one?

    Model answer

    O(n log n).

    Explanation

    Each insertion is O(log n), and there are n insertions, so total O(n log n).

  55. 55

    Explain the heap sort algorithm and its time complexity.

    Model answer

    Build a max heap from the array (O(n) using heapify from the bottom, but here we are inserting; typically heapify is O(n)). Then repeatedly extract the maximum and place it at the end of the array. Total O(n log n).

    Explanation

    Extracting n times costs O(n log n), so sorting is O(n log n).

  56. 56

    What are the two main components of a graph?

    Model answer

    Vertices (nodes) and edges (connections between vertices).

    Explanation

    Edges define relationships between vertices.

  57. 57

    What does it mean for two vertices to be adjacent in a graph?

    Model answer

    They are directly connected by an edge.

    Explanation

    Adjacent vertices are also called neighbors.

  58. 58

    What is an undirected graph?

    Model answer

    A graph where edges have no direction; the edge (u, v) is the same as (v, u), allowing traversal in both directions.

    Explanation

    Roads between cities are often modeled as undirected.

  59. 59

    What is a directed graph?

    Model answer

    A graph where edges have a direction; the edge from u to v does not imply an edge from v to u.

    Explanation

    Directed graphs are used for one-way streets, web links, etc.

  60. 60

    What is a weighted graph?

    Model answer

    A graph where each edge has a numerical weight (e.g., distance, cost).

    Explanation

    Weighted graphs are used in shortest path problems.

  61. 61

    What is a cyclic graph?

    Model answer

    A directed graph that contains at least one cycle (a path that starts and ends at the same vertex following edges).

    Explanation

    Undirected graphs always have cycles unless they are trees.

  62. 62

    What is a disconnected graph?

    Model answer

    A graph where at least one vertex is not reachable from another (i.e., there are isolated components).

    Explanation

    Not all vertices are connected by paths.

  63. 63

    Describe the adjacency matrix representation of a graph.

    Model answer

    A V x V matrix where entry [i][j] is 1 (or weight) if there is an edge from vertex i to vertex j, and 0 otherwise. For undirected graphs, the matrix is symmetric.

    Explanation

    It uses O(V^2) space, which is wasteful for sparse graphs.

  64. 64

    Describe the adjacency list representation of a graph.

    Model answer

    For each vertex, store a list (often a linked list) of its adjacent vertices (and edge weights if weighted).

    Explanation

    It uses O(V + E) space, more efficient for sparse graphs.

  65. 65

    What is hashing?

    Model answer

    Hashing is the process of passing data through a hash function to produce a unique output (digest) that represents the data.

    Explanation

    The output is often used as an index to store or retrieve the data quickly.

  66. 66

    What properties should a good hash function have?

    Model answer

    Deterministic (same input gives same output), fast to compute, and ideally produces unique outputs for different inputs (avoiding collisions).

    Explanation

    These properties enable O(1) average time for hash table operations.

  67. 67

    What is a hash collision?

    Model answer

    When two different inputs produce the same hash output (index).

    Explanation

    Collisions degrade performance; resolution strategies like chaining or open addressing are used.

  68. 68

    How does hashing enable constant-time operations in hash tables?

    Model answer

    By using a hash function to compute the index where an element should be stored; if the hash function is perfect and fast, lookups, insertions, and deletions take O(1) average time.

    Explanation

    The key directly maps to a location, bypassing the need for search.

1 of 68
Question 1 of 68

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent