Data Structures Fundamentals
- 110Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
- spaceFlip card
- KI knew it
- DI didn't know it
- ←Previous card
- →Next card
What is a data structure?
A way of organizing data so it can be accessed, queried, or updated efficiently.
Or swipe the card
110 cards
What is a data structure?
A way of organizing data so it can be accessed, queried, or updated efficiently.
Why are data structures important to algorithms and software?
They help create fast, powerful algorithms, organize data naturally, and make code cleaner and easier to understand.
What is an abstract data type (ADT), and how does it differ from a data structure?
An ADT specifies the behavior and interface a structure must provide; a data structure is a particular implementation of that interface.
How does the transportation analogy distinguish an ADT from an implementation?
Getting from point A to point B is the ADT; walking, biking, or taking a train are possible implementations.
Can one ADT have multiple valid implementations?
Yes. For example, a list can be implemented with a dynamic array or a linked list; a queue can even be implemented using stacks.
What two resource questions does computational complexity commonly address?
How much time an algorithm takes and how much space it requires.
What does Big O notation describe in this material?
An algorithm's worst-case growth in resource use as the input becomes arbitrarily large.
Why are constant factors and lower-order terms usually omitted in Big O?
For sufficiently large inputs, the dominant growth term determines the asymptotic order.
What does n usually represent in a complexity expression?
The size of the algorithm's input.
What is the worst-case time complexity of searching an unordered list for an absent value?
O(n).
What time complexity does binary search achieve, and why?
O(logn), because each iteration discards half of the remaining search range.
How are nested loops at different levels generally analyzed?
Multiply their iteration counts; add the work of separate loops that execute sequentially.
What are the complexities of generating all subsets of a set and all permutations of a string?
Generating all subsets takes O(2n); generating all permutations takes O(n!).
What are the stated complexities of merge sort and scanning an array with dimensions n by m?
Merge sort takes O(nlogn); visiting every cell in the array takes O(nm).
What is a static array?
A fixed-length container of indexable elements stored in a contiguous block of memory.
In a zero-based array of length n, what are the valid indices?
From 0 through n−1, inclusive.
Why can array indexing take constant time?
An array's indexable, contiguous layout lets the program directly access the requested position.
What are common uses of arrays?
Temporarily storing objects, buffering input or output, implementing lookup tables, returning multiple values through a reference, and caching subproblems in dynamic programming.
Why are arrays useful as buffers when processing a file too large to fit in memory?
They hold a small chunk at a time, allowing the program to process the file incrementally.
What are the typical access and search complexities for an array?
Indexed access is O(1); searching can take O(n) in the worst case.
How does a dynamic array typically grow when its internal array is full?
It allocates a larger internal array, often double the capacity, copies the existing elements, and then adds the new element.
Why is appending to a dynamic array usually described as constant time?
Most appends take constant time; occasional resizing and copying are infrequent, so append is constant time on average over many operations.
Why can inserting or deleting an element within an array take linear time?
Other elements may need to be shifted, and resizing may require copying elements.
What is a linked list?
A sequential collection of nodes, each holding data and links to other nodes.
What do the head and tail of a linked list refer to?
The head is the first node; the tail is the last node.
How do singly and doubly linked lists differ?
A singly linked node points to the next node; a doubly linked node points to both the next and previous nodes.
What is a key memory trade-off between singly and doubly linked lists?
Singly linked lists use less memory because each node needs only one link; doubly linked lists use extra memory for the previous link.
How are insertion and removal performed in the middle of a singly linked list?
Insertion changes neighboring next links; removal changes the preceding node's next link to skip the removed node.
What are typical linked-list operation complexities when head and tail references are maintained?
Searching and removing from the middle take O(n); adding at the head or tail and removing the head take O(1).
Why does removing the tail of a singly linked list take linear time, but removing it from a doubly linked list can take constant time?
A singly linked list must be traversed to find the node before the tail; a doubly linked list can follow the tail's previous link.
What is a stack, and what does LIFO mean?
A stack is a one-ended linear structure where insertion and removal occur at the top; LIFO means last in, first out.
What are the primary stack operations?
Push adds an element to the top; pop removes the top element; peek observes the top without removing it.
What are common uses of stacks?
Function-call tracking and recursion, undo or navigation behavior, matching brackets, and depth-first search.
How can a stack determine whether a bracket sequence is valid?
Push opening brackets; for each closing bracket, require a matching opening bracket at the top and pop it; finally, require the stack to be empty.
How do the pegs in the Tower of Hanoi relate to stacks?
Each peg behaves like a stack: only the top disk is moved, by popping it from one peg and pushing it onto another.
What is the time complexity of stack push, pop, and peek when implemented with a linked list?
Each takes O(1) when the top is directly referenced.
What is a queue, and what does FIFO mean?
A queue adds elements at the back and removes them from the front; FIFO means first in, first out.
What are enqueue, dequeue, and peek in a queue?
Enqueue adds at the back; dequeue or poll removes from the front; peek observes the front without removing it.
What are common applications of queues?
Modeling waiting lines, retaining a fixed number of recent items, managing server requests, and performing breadth-first search.
What is breadth-first search (BFS)?
A graph traversal that visits a starting node's neighbors first, then the neighbors of those nodes, expanding outward by distance.
How does BFS use a queue, and why mark nodes visited?
Enqueue and mark the start; repeatedly dequeue a node and enqueue each unvisited neighbor after marking it visited.
What are the typical complexities of queue enqueue, dequeue, and peek with endpoint references?
Each takes O(1); searching for or removing an arbitrary interior value can take O(n).
What is a priority queue?
An ADT in which each comparable element has a priority, and the highest-priority element is removed first.
Is a priority queue the same thing as a heap?
No. A priority queue is an ADT; a heap is one possible implementation.
What is the heap invariant?
Every parent is ordered with respect to its children: in a min-heap, parent values are no greater; in a max-heap, parent values are no less.
How do min-heaps and max-heaps differ?
A min-heap has the smallest value at the root; a max-heap has the largest value at the root.
What is the complete binary tree property used by a binary heap?
Every level except possibly the last is full, and nodes on the last level are as far left as possible.
How can a zero-based array represent a binary heap, and how are child indices calculated?
Store nodes level by level; for a node at index i, its left child is at 2i+1 and its right child at 2i+2.
How is a value inserted into a binary heap?
Place it at the next complete-tree position, then swim or bubble it upward until the heap invariant holds.
How is the root removed from a binary heap?
Swap the root with the last element, remove the last element, then sink or bubble down the new root.
What are the typical binary-heap complexities for peek, insertion, and removing the root?
Peek is O(1); insertion and root removal are O(logn).
Why can removing an arbitrary value from a basic binary heap take O(n)?
The heap does not directly reveal that value's position, so it may need a linear scan before removal.
How can a hash table improve arbitrary removal and containment checks in a heap?
Map each value to the heap index or indices where it occurs, updating the map when heap elements swap.
How can a min-priority queue be used when a max-priority queue is needed?
Reverse the comparison ordering, or for numeric values, negate values on insertion and negate them again when retrieved.
What is an indexed priority queue?
A priority queue that associates keys with values and supports efficient lookup, update, and deletion by key.
What maps does a binary-heap indexed priority queue maintain?
A values array indexed by key, a position map from key index to heap position, and an inverse map from heap position to key index.
What are the typical complexities of indexed priority-queue insertion, key update, and deletion by key?
They take O(logn), with key-to-position lookup taking O(1) before the heap is repaired.
What are increase-key and decrease-key operations?
Restricted updates that change a key's value only in the specified direction.
What is a union-find (disjoint-set) data structure?
A structure that tracks elements partitioned into disjoint groups, supporting find and union operations.
How does union-find support Kruskal's minimum spanning tree algorithm?
Process edges in ascending weight order; use find to detect whether endpoints are already connected, and union their groups when they are not.
What is a minimum spanning tree?
A minimum-cost subset of a graph's edges that connects all vertices.
How does path compression improve union-find?
During find, redirect nodes along the traversed path to the component's root.
What does the union-by-size strategy do?
It attaches the smaller component's root beneath the larger component's root.
How can an array represent union-find parent links?
At each index, store its parent index; a root is identified when its parent is itself.
What is a tree in graph terms?
A connected, acyclic undirected graph; equivalently, a tree with n nodes has n−1 edges.
What are the root, parent, child, leaf, and subtree of a rooted tree?
The root is the top node; a parent is directly above a child; a leaf has no children; a subtree is a node and its descendants.
What makes a tree a binary tree?
Each node has at most two children.
What is the binary search tree invariant?
Values in a node's left subtree are smaller than the node; values in its right subtree are larger.
What is the average and worst-case search complexity of an ordinary binary search tree?
Average operations are typically O(logn) for suitably distributed data; worst-case operations can be O(n).
How is a value inserted into a binary search tree?
Start at the root, compare the value at each node, and descend left for a smaller value or right for a larger one until reaching an empty position.
How is a binary search tree node with two children removed?
Replace its value with either the largest value in its left subtree or the smallest value in its right subtree, then remove that successor node.
What does an inorder traversal produce when applied to a binary search tree?
The stored values in increasing order.
How do preorder and postorder traversals differ?
Preorder visits the node before its subtrees; postorder visits the node after its subtrees.
How is a level-order tree traversal performed?
Use a queue to process nodes one level at a time, adding each processed node's children.
What is a balanced binary search tree, and why is it useful?
A self-adjusting search tree that maintains logarithmic height, ensuring logarithmic worst-case operations.
Why do tree rotations preserve the binary search tree invariant?
They rearrange links while retaining the relative ordering of values in the affected subtrees.
What is an AVL tree's balance factor?
The right-subtree height minus the left-subtree height.
What AVL imbalance cases are repaired with rotations?
Left-left uses a right rotation; left-right uses a left rotation on the child then a right rotation; right-right uses a left rotation; right-left uses a right rotation on the child then a left rotation.
What must an AVL tree update after insertion or deletion?
Affected node heights and balance factors, followed by rotations where needed.
What is a hash table?
A data structure that maps unique keys to associated values using hashing.
What is a hash function?
A function that maps a key to an integer used to choose a table position.
What properties should a hash function have?
It must be deterministic, and a good hash function distributes keys uniformly to reduce collisions.
What is a hash collision?
A collision occurs when two distinct keys produce the same hash or table position.
What does an equal hash value tell you about two keys?
The keys might be equal, but equality must still be checked directly.
Why do hash-table keys commonly need to be immutable?
Changing a key can change its hash, making the stored entry difficult to locate.
What are separate chaining and open addressing?
Separate chaining stores colliding entries in an auxiliary structure at a table position; open addressing probes for another position within the table.
How does separate chaining handle insertion and lookup after a collision?
It follows the bucket's chain, compares keys, then adds a new entry or retrieves the matching entry.
What is a hash table's load factor?
The number of stored items divided by the table capacity.
Why does a hash table resize when it exceeds a load threshold?
To reduce crowding and maintain efficient operations; entries are rehashed into a larger table.
What are the average and worst-case complexities of hash-table insertion, lookup, and removal?
With good distribution, operations are average O(1); with severe collisions, they can be O(n).
How does open addressing resolve a collision?
It uses a probing sequence to check alternative table positions until an available slot or the desired key is found.
How do linear probing, quadratic probing, and double hashing differ?
They use, respectively, a linear offset, a quadratic offset, or an offset based on a second hash function.
What is primary clustering in linear probing?
A run of occupied slots can grow, causing later probes to encounter increasingly long clusters.
Why must an open-addressing probing sequence be chosen carefully?
A sequence that cycles through only some slots can fail to reach an empty slot, even when other slots are free.
How does open addressing handle deletion without breaking future searches?
It marks the deleted slot with a tombstone rather than making it empty.
How can tombstones be removed or reused?
Insertion can reuse a tombstone, and resizing removes old tombstones when live entries are rehashed.
What is the Fenwick tree also called, and what does it support?
It is also called a binary indexed tree; it supports array prefix or range-sum queries and point updates.
Why can prefix sums alone be inefficient when array values are updated?
Changing one value may require recomputing many later prefix sums.
How does a Fenwick tree answer a prefix-sum query?
Starting at the requested index, add the stored partial sums and repeatedly subtract the index's least significant set-bit value until reaching zero.
How is a range sum from index i through index j obtained from Fenwick prefix sums?
Subtract the prefix sum ending just before i from the prefix sum ending at j.
How does a Fenwick tree perform a point update?
Add the change to the target cell and repeatedly advance by the current index's least significant set-bit value, updating each responsible cell.
What are the typical Fenwick tree construction and operation complexities?
Linear-time construction is possible; point updates and prefix or range queries take O(logn).
What is a suffix of a string?
A nonempty substring that begins at some position and extends to the end of the string.
What does a suffix array store?
The starting indices of all suffixes of a string, sorted lexicographically.
What does the LCP array record?
For adjacent suffixes in suffix-array order, it records the number of initial characters they share.
How can suffix and LCP arrays identify the longest repeated substring?
Find the maximum LCP value; the corresponding adjacent suffixes share the longest repeated prefix.
How can the number of distinct substrings be computed from a suffix array and LCP array?
Subtract the sum of the LCP values from the total number of substrings, 2n(n+1).
How can a suffix array solve the longest common substring problem for multiple strings?
Concatenate the strings with distinct low-valued sentinels, build suffix and LCP arrays, then find a window containing suffixes from at least k source strings and maximize its minimum relevant LCP.
How is a sliding window used to find a longest common substring shared by at least k strings?
Track which source strings occur in the window; expand until at least k colors are present, then shrink while that condition remains true and evaluate the window's minimum LCP.
How many nonempty substrings does a string of length n have?
2n(n+1).

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