ADS Logs
An algorithm is a step-by-step instruction that solves a problem in bounded time; its cost is measured by counting primitive operations — the small steps whose duration does not depend on the input size.
The definition carries a requirement that is easy to read past: bounded time...
Q What is an array-list, and why is add usually $O(1)$ but occasionally $O(n)$?
An array-list wraps a plain array and resizes it automatically; an add into a free slot is $O(1)$, but an add into a full array first copies every element into a bigger one, which is $O(n)$.
* The fourth add fits; the fifth finds the array full and pays for copying all four firs...
Q Why does an array-list grow by doubling its capacity rather than by adding one slot at a time?
Doubling makes the expensive copies so rare that their total cost is proportional to the number of adds — on average each add costs $O(1)$ (amortised). Growing by a constant amount makes every few adds pay a full copy, giving $O(n^2)$ for $n$ adds.
* The spikes get taller but tw...
Q How do you insert a node v after a given node u in a doubly-linked list?
Remember u's old successor w ← u.next, point v at both neighbours (v.prev ← u, v.next ← w), then point the neighbours at v (w.prev ← v, u.next ← v) — four reference writes, $O(1)$.
* v is wired to both neighbours first; only then are u and w redirected to v. *
Algorithm addAfter...
Q Starting from an empty stack, what do the calls push(5), push(3), pop(), push(7), push(9), top(), po...
pop() → 3, top() → 9, pop() → 9, pop() → 7; the stack ends as (5).
* Each pop takes the most recent push that is still there. *
Trace it, bottom on the left, top on the right:
Operation
Returns
Stack
push(5)
—
(5)
push(3)
—
(5, 3)
pop()
3
(5)
push(7)
—
(5, 7)
push...
Q What is a deque (double-ended queue), and what are its operations?
A deque ("deck") allows inserting and removing at both ends: addFirst, addLast, removeFirst, removeLast, plus first() and last() to look without removing; reading from an empty deque returns null.
* A stack uses one end, a queue uses each end for one job, a deque uses both ends...
Q What is the height of a node and of a tree, and how is it computed?
The height of a leaf is 0; the height of an internal node is 1 + the maximum height of its children. The height of a tree is the height of its root, which equals the depth of its deepest node.
* Depth is measured down from the root; height is measured up from the deepest leaf be...
Q What are the strengths and the weakness of storing a binary tree in an array?
Strengths: no references to store, and every child or parent is found by index arithmetic in $O(1)$. Weakness: the array needs a slot for every rank up to the highest one, so an unbalanced tree wastes enormous amounts of space — up to $2^n$ slots for $n$ nodes.
The rank of a node...
Q How is an arithmetic expression tree evaluated, and why is that a postorder traversal?
Recursively: a leaf returns its number; an internal node first evaluates its left and right subtree, then applies its operator to the two results. The node is handled after its children — postorder. For $((2 × (5 − 1)) + (3 × 2))$ the result is 14.
* Values flow upwards: 5 − 1 =...
Q What is a singly-linked list, and what does each node store?
A chain of nodes where each node holds one element and a reference to the next node; the last node points to null, and the list keeps a reference to the first node (the head) as its entry point.
A node is a tiny object with exactly two fields:
public class Node {
private Elemen...