VelocerOpen the app

Velocer › Flashcards › University of Illinois Urbana-Champaign › CS 225: Data Structures · UIUC

CS 225: Data Structures · UIUC

My CS 225 cards from this semester, mostly data structures. good to run through before the midterm. · by illini_index · 180 cards

University of Illinois Urbana-Champaign

Study this deck Embed

  1. What does stack memory hold, and how is it managed?It holds a function's locals and parameters in its stack frame; it is managed automatically, fast, and small.
  2. How is heap memory allocated and freed in C++?Manually, using new and delete. It is slower and larger than stack memory.
  3. When should you use heap memory instead of stack memory?When a value must outlive the function that created it.
  4. What is the difference between passing by value, by pointer, and by reference?By value passes a local copy; by pointer passes the address of the original; by reference passes an alias for the original.
  5. What is a pointer?A variable that holds a memory address.
  6. What is a reference?A second name (alias) for an existing variable.
  7. What is the ownership test for objects?Object A owns B when destroying A also destroys B.
  8. What kind of copies do the default copy constructor and assignment operator make?Shallow copies.
  9. What is the Rule of Three?If a class needs a custom destructor, copy constructor, or copy assignment operator, it needs all three.
  10. What is the Rule of Zero?Classes that own nothing should define none of the destructor, copy constructor, or copy assignment operator.
  11. What are templates in C++?Code recipes over a generic type; the compiler generates real code only for the types actually used.
  12. How many versions of a template function like T sum(T a, T b) does the compiler generate?Only one per type actually used, rather than separate hand-written int and float versions.
  13. What is an abstract data type (ADT)?A description of the stored data and the valid operations on it, independent of any implementation.
  14. What are the two common implementations of a list ADT?The linked list and the array list.
  15. What does it mean that a list is ordered?The items have a defined sequence or position, not necessarily sorted by value.
  16. Why is an ADT independent of implementation?Because it specifies only the data and valid operations, leaving the underlying implementation choice open.
  17. What two fields does a ListNode store, and how do they differ?It stores data (by reference: cannot be null, no pointer overhead) and next (by pointer: can change, can be null).
  18. What movement does a singly linked list allow?Only forward movement.
  19. How does insertAtFront work and what is its time complexity?Create a node, point its next at the current head, then move head to the new node. It is O(1).
  20. What does _index(i) return, and why return that type?It returns a ListNode*&—a reference to the pointer that leads to position i—so callers can rewire that link directly. Returning only ListNode* would not allow changing the link.
  21. What is the time complexity of _index(i)?O(n).
  22. How does insert(data, i) work and what is its overall complexity?Get the link to position i, create a node, set its next to the old node, and redirect the link. It is O(n) overall due to indexing.
  23. Why does operator[] return T&?So the same call supports both reading and writing the element.
  24. What are linked lists good at, and what are they bad at?They are good at modifying when you already hold the location, but bad at random access.
  25. What three values describe an array list?The start of the data, the size (next free slot), and the capacity (one past the end).
  26. What is the time complexity of accessing an array list element by index?O(1).
  27. Why are insertFront and insert-at-index O(n) for an array list?Because existing elements must be shifted to make room.
  28. What is the time complexity of insertBack when the array is not full?O(1).
  29. What happens to total copy cost if you resize by adding a constant (+2) each time?Total copies grow quadratically, so the amortized cost per insert is O(n).
  30. How many reallocations occur when resizing by doubling for N inserts?About ceil(log2 N) reallocations.
  31. What is the total number of copies for N inserts when doubling?2N - 1 copies.
  32. What is the amortized cost per insert when resizing by doubling?O(1) amortized per insert, even though a single insert can cost O(N).
  33. Why can one insert cost O(N) yet still be O(1) amortized with doubling?Because expensive reallocations happen rarely, so the total work across many inserts divided by the number of inserts is constant.
  34. What does LIFO stand for, and which data structure uses it?LIFO means Last In, First Out. A stack uses it: the most recently pushed element is the first popped.
  35. What does FIFO stand for, and which data structure uses it?FIFO means First In, First Out. A queue uses it: the earliest enqueued element is the first dequeued.
  36. In a circular array queue, what state variables are kept?front, size, and capacity.
  37. How does enqueue work in a circular array queue?Place the element at index (front + size) mod capacity, then increment size (if not full).
  38. How does dequeue work in a circular array queue?Take the element at front, set front = (front + 1) mod capacity, then decrement size (if not empty).
  39. What special step is needed when resizing a circular queue?The elements must be unwrapped (copied out in logical order) into the new, larger array.
  40. What is the purpose of iterators?They let code loop over any container without knowing its internal structure.
  41. Which two methods does a container supply for iteration, and what do they mean?begin() returns an iterator to the first element; end() returns an iterator to one past the last element.
  42. What does operator* do on an iterator?It dereferences the iterator to access the element it currently points to.
  43. Why does end() point one past the last element rather than at the last element?It serves as a sentinel so a loop can compare against it and stop after visiting the final element.
  44. What is the root of a tree?The topmost node of a tree; it has no parent.
  45. What is a leaf node?A node with no children.
  46. What is a subtree?A node together with all of its descendants, which itself forms a tree.
  47. What is the height of an empty tree?-1.
  48. What is the recursive formula for the height of a binary tree?height(T) = 1 + max(height(T_L), height(T_R)).
  49. What is a full binary tree?A binary tree in which every node has either 0 or 2 children.
  50. What is a perfect binary tree?A binary tree where all leaves are at the same depth and every internal node has 2 children.
  51. How many leaves does a perfect binary tree of height h have?2^h leaves.
  52. What is a complete binary tree?A tree that is perfect except the last level, which is filled from left to right.
  53. What is the order of nodes in a pre-order traversal?Node, then left subtree, then right subtree; it always starts at the root.
  54. What is the order of nodes in an in-order traversal?Left subtree, then node, then right subtree; it starts at the leftmost node.
  55. What is the order of nodes in a post-order traversal?Left subtree, then right subtree, then node; the root is visited last.
  56. How does traversal differ from search?Traversal visits all nodes in O(n); search looks for a target and can stop early.
  57. What data structure does depth-first search (DFS) use?A stack, or recursion.
  58. What data structure does breadth-first search (BFS) / level-order traversal use?A queue.
  59. How does BFS/level-order traversal order its visits?It finishes level i before moving to level i+1.
  60. What is the binary search tree (BST) property?Every key in the left subtree is smaller than the root's key, and every key in the right subtree is larger than the root's key.
  61. How does the find operation work in a BST?Compare the target with the root, then go left or right accordingly; the base case is reaching a null pointer.
  62. Why does the BST find helper return a TreeNode*& (a reference to a pointer)?So that insert can reuse the helper and directly modify the pointer where the new node should be attached.
  63. How do you remove a leaf node (no children) from a BST?Simply delete the node and set its parent's pointer to null.
  64. How do you remove a node with one child from a BST?Splice the child up to replace the removed node, connecting it to the node's parent.
  65. How do you remove a node with two children from a BST?Swap the node's key with its in-order predecessor, then remove that predecessor node (which has at most one child).
  66. What is the time complexity of all BST operations?O(h), where h is the height of the tree.
  67. What happens to a BST's height when you insert sorted data?The tree becomes path-shaped with h = n-1, giving worst-case O(n) operations.
  68. What is the worst-case time complexity of BST operations and why?O(n), because inserting sorted data produces a path-shaped tree of height n-1.
  69. What is the base case of the BST find operation?Reaching a null pointer, meaning the key is not in the tree.
  70. In a BST, which direction do you go when the target key is smaller than the current node's key?Go to the left subtree.
  71. In a BST, which direction do you go when the target key is larger than the current node's key?Go to the right subtree.
  72. What is the in-order predecessor of a node?The node with the largest key smaller than the node's key, found by going left once then right as far as possible.
  73. Why is the in-order predecessor used when removing a two-child node?Swapping with it preserves the BST property, and the predecessor has at most one child, making removal easy.
  74. How is a k-d tree constructed?Split at the median along one dimension, then recurse on each side while cycling through dimensions.
  75. What is the worst-case time complexity of nearest-neighbor search in a k-d tree?O(N) — it can touch every point in the worst case.
  76. Why use quickselect instead of sorting to find medians when building a k-d tree?Sorting to find each median costs about O(n log² n) total, while quickselect finds the k-th smallest without fully sorting.
  77. Describe the quickselect algorithm.Choose a random pivot, move it to the end, partition into smaller and larger elements, place the pivot in its final spot, then recurse only into the side containing index k.
  78. What is the average and worst-case time complexity of quickselect?Average O(N), worst case O(N²).
  79. What does quickselect find?The k-th smallest element in an array, without fully sorting it.
  80. In k-d tree construction, how are dimensions handled across recursion levels?Dimensions are cycled through, so each level splits along a different dimension.
  81. What split point is used at each node when building a k-d tree?The median along the current dimension.
  82. What is the balance factor formula for a node in an AVL tree?b = height(T_R) − height(T_L), the right subtree height minus the left subtree height.
  83. What balance factor values can every node in an AVL tree have?Every AVL node has balance factor b ∈ {−1, 0, 1}.
  84. What are the four rotation types used to rebalance an AVL tree?Left, right, left-right, and right-left rotations.
  85. What properties do AVL rotations have?They are O(1), local, preserve the BST property, and reduce the subtree height by one.
  86. What are the steps of a left rotation?Save the root as tmp, make the middle node the new root, set tmp->right = new root->left, then set new root->left = tmp.
  87. When a node has balance factor +2 (right-heavy), which rotation should be performed?Check the right child: if its balance is +1, do a left rotation; otherwise do a right-left rotation.
  88. How is the rotation choice mirrored for a node with balance factor −2?Check the left child: if its balance is −1, do a right rotation; otherwise do a left-right rotation.
  89. How many imbalances can an AVL insert create, and how many can a remove create?Insert can create at most one imbalance; remove may require fixes at every level up the path.
  90. What are the time complexities of find, insert, and remove in an AVL tree?All are O(h), and since h = O(log n), they are O(log n).
  91. Why does bounding an AVL tree's height from above correspond to bounding its node count from below?Because a taller AVL tree requires at least N(h) nodes; showing N(h) grows exponentially in h limits h in terms of n.
  92. Why are AVL tree operations guaranteed to be O(log n)?The AVL balance condition ensures height h = O(log n), and find, insert, and remove are all O(h).
  93. Why does Big-O analysis alone fail to capture the cost of B-Tree operations?Because Big-O assumes uniform operation cost, but disk seeks and network fetches are orders of magnitude slower than cache hits, so minimizing seeks matters more than minimizing raw operation counts.
  94. What are the three design goals of B-Trees?Few seeks, keeping data local, and making every fetched block relevant.
  95. Define a B-Tree of order m.Keys in each node are sorted; a node holds at most m-1 keys and an internal node has one more child than it has keys; all leaves are on the same level.
  96. In a B-Tree of order m, what is the maximum number of keys a node can hold?At most m-1 keys.
  97. What is the relationship between keys and children in an internal node of a B-Tree?An internal node has exactly one more child than it has keys.
  98. What constraint applies to the leaves of a B-Tree?All leaves are on the same level.
  99. How many children can the root of a B-Tree have?The root is a leaf, or it has between 2 and m children.
  100. How many children can a non-root internal node of a B-Tree have?Between ceil(m/2) and m children.
  101. Describe the insertion process in a B-Tree.Add the key to the correct leaf; when a node reaches m keys, split it and push the middle key up, which may cascade all the way to the root.
  102. What happens when a B-Tree node reaches m keys during insertion?It is split and the middle key is pushed up to the parent, potentially cascading to the root.
  103. How does the find operation work in a B-Tree?Search the node's sorted array; return on a match, otherwise recurse into the child before the first larger key.
  104. What bounds the number of seeks in a B-Tree search?The number of seeks is bounded by the tree's height, which is O(log_m n).
  105. What is the height of a B-Tree of order m with n keys?O(log_m n).
  106. What technique is used to analyze B-Tree height?Counting the minimum number of keys at each level.
  107. Why is a high branching factor (large m) desirable in a B-Tree?It reduces the tree height, and therefore the number of disk seeks needed to reach a leaf.
  108. Why are sorted or unsorted arrays and lists inefficient for priority queues?With sorted or unsorted arrays and lists, one of the operations takes O(n) time.
  109. What is a min-heap?A complete binary tree where each root is smaller than the roots of its subtrees, and those subtrees are also min-heaps.
  110. In an array-based heap stored from index 1, how do you find the parent and children of index i?Parent = floor(i/2), left child = 2i, right child = 2i+1, all in O(1) time.
  111. How does insert work in a min-heap?Place the new element at the end (growing the array if full), then heapifyUp by swapping with the parent while it is smaller. This takes O(log n).
  112. What is heapifyUp?Swapping a newly inserted element upward with its parent while it is smaller than the parent, restoring the heap property in O(log n).
  113. How does removeMin work in a min-heap?Save the root, move the last element to the root, shrink the array, then heapifyDown by swapping with the smaller child. This takes O(log n).
  114. What is heapifyDown?Swapping an element downward with its smaller child until the heap property is restored, taking O(log n).
  115. What is the time complexity of insert and removeMin in a min-heap?Both run in O(log n) time.
  116. How does buildHeap work and what is its time complexity?Run heapifyDown from the last parent down to index 1; this builds a heap in O(n) time.
  117. Why is buildHeap O(n) rather than O(n log n)?Because heapifyDown is run starting from the last parent down to index 1, and most nodes are near the bottom where little work is needed, giving a total of O(n).
  118. What property must a min-heap satisfy at every node?Each node's value is smaller than the values at the roots of its subtrees.
  119. What does the root of a min-heap contain?The minimum element, which is why removeMin can retrieve it directly.
  120. What does it mean for two elements to be in the same disjoint set?They have the same representative, so find(a) == find(b). Each element belongs to exactly one set.
  121. What are the three core operations on a disjoint-set (union-find) structure?makeSet, union, and find.
  122. In an UpTree array representation of disjoint sets, what does a value of -1 (or a negative size) at an index mean?That index is a root of its set. Non-root entries store the index of their parent.
  123. How does union by size work?Store the negative size at each root, and when merging, attach the smaller tree under the larger root.
  124. What is the purpose of union by height (rank)?It keeps the tree height small by attaching the shorter tree under the taller one, limiting growth of tree depth.
  125. What is path compression and when is it applied?During a find, every visited node is re-pointed directly to the root. This flattens the tree and speeds up future operations.
  126. What is the time complexity of a sequence of operations with smart union plus path compression?Nearly constant time per operation, bounded by the iterated logarithm log* n.
  127. What does the iterated logarithm log* n count?The number of times you apply the logarithm before the value drops to 1 or below.
  128. How can you test whether two elements a and b are in the same equivalence class using union-find?Compare their representatives: if find(a) == find(b), they are in the same set.
  129. What does the find operation return in a disjoint-set structure?The representative (root) of the set containing the given element.
  130. In the UpTree array, what is stored at a non-root index?The index of that node's parent.
  131. Why is representing an equivalence relation with disjoint sets valid?Because an equivalence relation partitions elements into disjoint classes, and each element is in exactly one set with one representative.
  132. What trade-off does union by size address compared to naive union?Naive union can create tall, thin trees; union by size keeps trees balanced so find stays efficient.
  133. What is the key difference between union by size and union by rank/height?Union by size compares the number of elements (stored as negative size at roots), while union by rank/height compares tree heights and attaches the shorter tree under the taller.
  134. What does the notation \(G=(V,E)\) represent in graph theory?A graph \(G\) consisting of a set of vertices \(V\) and a set of edges \(E\).
  135. What is a path in a graph?A sequence of vertices connected by edges.
  136. What is a cycle in a graph?A path that starts and ends at the same vertex.
  137. What is a simple graph?A graph with no self-loops and no multi-edges.
  138. What is a subgraph?A subset of a graph's vertices and edges, where every included edge connects only included vertices.
  139. For a simple connected graph with \(n\) vertices and \(m\) edges, what bounds apply to \(m\)?\(n-1 \le m \le n(n-1)/2\).
  140. What is the relationship between the sum of vertex degrees and the number of edges \(m\)?The sum of all vertex degrees equals \(2m\).
  141. Which graph representation gives \(O(1)\) areAdjacent queries?The adjacency matrix.
  142. Which graph representation gives \(O(1)\) insertVertex and \(O(\deg v)\) incidentEdges?The adjacency list.
  143. Which graph representation has removeVertex costing \(O(n+m)\)?The edge list.
  144. What is the difference between a discovery edge and a cross edge in BFS?A discovery edge leads to a new (unvisited) vertex; a cross edge leads to an already visited vertex. The presence of a cross edge indicates a cycle exists.
  145. What is the time complexity of BFS on a graph with n vertices and m edges?O(n + m).
  146. Why does BFS loop over all vertices rather than starting from a single source?To ensure disconnected parts of the graph are also covered.
  147. What structure do BFS discovery edges form?A spanning tree (or spanning forest if the graph is disconnected).
  148. What does BFS guarantee about shortest paths?BFS gives shortest paths in terms of edge count.
  149. In BFS, how do the depths of the two endpoints of a cross edge compare?They differ in depth by at most one.
  150. State the partition (cut) property.For any split of the vertex set into two sets, a minimum-weight edge crossing the split belongs to some MST.
  151. How does Kruskal's algorithm build an MST?It takes edges in order of increasing weight (via a priority queue or sorted array) and adds an edge if its endpoints are in different disjoint sets, stopping at n-1 edges.
  152. When does Kruskal's algorithm reject an edge?When the edge's endpoints are already in the same disjoint set (adding it would form a cycle).
  153. How does Prim's algorithm grow an MST?It grows a single tree from a start vertex, repeatedly removing the closest outside vertex from a priority queue and updating its neighbors' distances.
  154. How does Dijkstra's algorithm differ from Prim's algorithm?Dijkstra has the same shape as Prim, but the stored distance is the total path length from the source rather than distance to the growing tree.
  155. What key assumption does Dijkstra's algorithm make?It assumes there are no negative edge weights.
  156. What is the output of Dijkstra's algorithm?A DAG of shortest paths.
  157. What is the difference between average-case analysis and expected running time?Average-case analysis assumes a random input distribution; expected running time comes from randomness inside the algorithm itself.
  158. What defines a Las Vegas algorithm?It is always correct but its running time is random.
  159. What defines a Monte Carlo algorithm?It runs in bounded time but may be wrong with some probability.
  160. Name the four probabilistic outcome categories.True positive, false positive, true negative, and false negative.
  161. What three components make up a hash table?A hash function (key to integer, compressed to table size), an array, and a collision strategy.
  162. What properties make a good hash function?It should be deterministic, fast, and spread keys uniformly across slots.
  163. When is perfect hashing easy to achieve?Only when the keyspace is small and fully known in advance.
  164. What is SUHA (simple uniform hashing assumption)?The assumption that each key is equally likely to land in any slot.
  165. How is the load factor α of a hash table defined?α = n/N, the number of stored keys divided by the number of slots.
  166. How does separate chaining handle collisions, and what is its expected search cost?Each slot holds a list of colliding keys; expected search cost is about 1+α under SUHA, with worst case O(n).
  167. What is the main drawback of linear probing?It causes clustering, where occupied slots bunch together and degrade performance.
  168. What is the double hashing probe sequence formula?h(k,i) = (h₁(k) + i·h₂(k)) mod N.
  169. What advantage do hash tables have over BSTs/AVL trees, and what do they lose?They are faster for dictionary operations but lose ordering, so range queries are not supported.
  170. What is a Bloom filter composed of?A bit vector of length m plus k hash functions. Inserting sets k bits, and finding checks those k bits.
  171. What kind of errors can a Bloom filter produce?It can produce false positives but never false negatives.
  172. Why does a Bloom filter not support deletion?Because the bits are shared by many keys, so clearing bits for one key could affect others.
  173. What is the time cost of insert and find in a Bloom filter?O(k), where k is the number of hash functions.
  174. What is the false positive rate of a Bloom filter after n inserts?Approximately (1 - e^(-kn/m))^k, where m is the bit vector length and k is the number of hash functions.
  175. What is the optimal number of hash functions for a Bloom filter?k = (m/n) ln 2, where m is the bit vector length and n is the number of inserts.
  176. How is the union of two Bloom filters computed?By taking the bitwise OR of their bit vectors.
  177. How is the intersection of two Bloom filters approximated?By taking the bitwise AND of their bit vectors.
  178. What is cardinality in the context of sketches?The number of distinct items in a set.
  179. What is the Jaccard similarity formula for sets A and B?J(A,B) = |A ∩ B| / |A ∪ B|.
  180. What does a MinHash sketch keep?The k smallest hash values of a set, built from a set, a value k, and a hash function.

Velocer is free: spaced repetition and your own cards from any document.

PDF to flashcards · Lecture to flashcards · Practice exam · Syllabus study plan · Anki import

More decks from University of Illinois Urbana-Champaign