0% found this document useful (0 votes)
10 views4 pages

20 Hard Algorithm Questions

The document presents 20 hard algorithm-theoretical interview questions along with concise answers that cover key concepts in algorithms and theoretical computer science. Topics include lower bounds for sorting, NP-completeness, amortized analysis, and various algorithms for graph problems. Each question is designed to test understanding of fundamental principles and problem-solving techniques in the field.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

20 Hard Algorithm Questions

The document presents 20 hard algorithm-theoretical interview questions along with concise answers that cover key concepts in algorithms and theoretical computer science. Topics include lower bounds for sorting, NP-completeness, amortized analysis, and various algorithms for graph problems. Each question is designed to test understanding of fundamental principles and problem-solving techniques in the field.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

20 Hard Algorithm-Theoretical Interview Questions (with Answers)

Domain: Algorithms / Theoretical Computer Science

Each question is followed by a concise, clear answer that explains the approach, correctness,
and complexity where applicable.

1. Prove that comparison-based sorting has an Omega(n log n) lower bound.


Answer: Model comparison sorts as decision trees where each internal node is a comparison
and leaves are permutations. A correct sort must distinguish all n! permutations, so the tree has
at least n! leaves. A binary tree of height h has at most 2^h leaves, so 2^h >= n! => h >= log2(n!)
= Theta(n log n) (using Stirling's approximation). Thus any comparison sort has worst-case
Omega(n log n) comparisons.

2. Describe and prove correctness of the median-of-medians linear-time


selection algorithm.
Answer: Partition input into groups of 5, find medians of each group (O(n)), recursively find
median of medians M, partition array around M. Show at least 3n/10 elements are > M and at
least 3n/10 are < M, so recurrence T(n) <= T(n/5) + T(7n/10) + O(n) => T(n)=O(n). Correctness:
the pivot M guarantees a constant fraction split, ensuring linear time; selection correctness
follows from standard partition logic.

3. Explain NP, co-NP, and the significance of NP-completeness. Give an example


of an NP-complete problem and sketch the reduction.
Answer: NP = decision problems verifiable in polynomial time given a certificate. co-NP =
complements of NP problems. NP-completeness: a problem in NP is NP-complete if every
problem in NP reduces to it in polynomial time. Example: 3-SAT is NP-complete (Cook-Levin).
Sketch: reduce SAT to 3-SAT via clause transformations or show Boolean circuit satisfiability
reduces to 3-SAT, encoding variables/clauses so that satisfying assignment corresponds. NP-
complete implies likely no polynomial-time algorithm unless P=NP.

4. Prove that the decision version of Subset Sum is NP-complete.


Answer: Subset Sum is in NP (certificate: subset). Reduce from Partition or from 3-Dimensional
Matching; a common reduction constructs numbers whose digits encode variable assignments
and clause satisfaction; ensure target sum corresponds to satisfying assignment. Hence NP-hard;
in NP, so NP-complete.
5. Explain the concept of amortized analysis and show that dynamic array
resizing by doubling has amortized O(1) append.
Answer: Amortized analysis averages cost over sequence of operations. For doubling arrays:
occasional expensive resize costs O(n) but happens infrequently. Using aggregate method: after
each resize to capacity 2^k, next 2^k appends are cheap (O(1)). Total cost for n appends <= O(n)
for copies + O(n) basic ops => amortized O(1) per append. Accounting/potential method yields
same result.

6. Give a formal proof that a directed graph has a topological order iff it is
acyclic.
Answer: If graph has topological order then edges go forward—no cycles. Conversely, if DAG,
perform induction: there exists a vertex with in-degree 0 (otherwise cycle). Remove it and
recursively topologically sort remaining graph, place removed vertex first. Thus DAG has
topological ordering.

7. Design and analyze an algorithm to find all bridges (cut edges) in an


undirected graph.
Answer: Use DFS with discovery times disc[u] and low[u] = min(disc[u], min(disc[v] for back
edges), min(low[w] for tree children w)). An edge (u,v) is bridge if low[v] > disc[u]. Run DFS from
each unvisited node; complexity O(V+E). Correctness follows because if low[v] > disc[u], v and its
subtree have no back-edge to u or ancestors, so removing (u,v) disconnects.

8. Prove correctness and give runtime of Dijkstra's algorithm; state necessary


conditions.
Answer: Dijkstra assumes non-negative edge weights. Use induction: when a vertex u is
extracted from the priority queue, its tentative distance equals the shortest path distance
because any shorter path would have to go through vertices with dist >= dist[u], contradicting
the min-heap extraction order. Complexity: with binary heap O((V+E) log V), with Fibonacci heap
O(V log V + E).

9. Explain reductions between decision and optimization problems — how to


use binary search or parametric search to solve optimization variants using
decision subroutines.
Answer: Many optimization problems can be solved by repeating a decision problem with
chosen parameter (binary search) if objective is monotone (e.g., minimum r such that property
holds). Parametric search generalizes when monotonicity and parallelizability exist. Complexity
multiplies decision cost by log(range/precision). Example: minimum bottleneck spanning tree via
threshold decision.
10. Show that finding a simple cycle of length >= k is NP-complete when k is
part of input.
Answer: Decision problem 'Does G have a simple cycle of length >= k?' is in NP. For NP-hardness
reduce from Hamiltonian Cycle: set k = |V|; Hamiltonian cycle exists iff there's a cycle of length
>= |V|. Since Hamiltonian Cycle is NP-complete, the general problem is NP-complete.

11. Explain the PCP theorem and its significance for hardness of approximation.
Answer: PCP theorem: NP = PCP(O(log n), O(1)), meaning NP statements can be probabilistically
checked by reading O(1) bits with O(log n) randomness. Consequence: some optimization
problems (e.g., MAX-3-SAT, Clique) are NP-hard to approximate within certain factors. It
underpins many inapproximability proofs by gap-introducing reductions.

12. Design an algorithm to compute strongly connected components and prove


its correctness and complexity.
Answer: Kosaraju's algorithm: run DFS and record finish times, compute transpose graph,
process vertices in decreasing finish time doing DFS on transpose to collect SCCs. Correctness:
finish time order ensures roots of SCCs discovered first in transpose. Complexity O(V+E). Tarjan's
algorithm uses one-pass DFS with lowlinks to find SCCs in O(V+E) as well.

13. Prove that any comparison-based decision tree solving element uniqueness
must take Omega(n log n) in the algebraic decision tree model.
Answer: Element uniqueness can be reduced to sorting: given n elements, to check uniqueness
you can sort (comparison model) then scan for duplicates. For decision-tree lower bound:
number of distinct outcomes (total orders with potential equalities) implies Omega(n log n)
comparisons. Precise argument uses information theory and counting permutations when
elements are distinct.

14. Explain the max-flow min-cut theorem and prove integrality for integer
capacities.
Answer: Max-flow min-cut: value of max s-t flow equals capacity of minimum s-t cut. Proof via
augmenting paths and residual graph: when no augmenting path exists, partition reachable
vertices from s in residual graph yields cut with capacity equal to current flow. Integrality: if
capacities are integers, each augmentation can be by at least 1 using Ford-Fulkerson with
integral augmenting amounts; thus max flow is integral. Use Edmonds-Karp (BFS) to bound
iterations O(VE).

15. Describe and analyze the algorithm for finding articulation points (cut
vertices).
Answer: Use DFS with discovery times disc[u] and low[u]. For root of DFS tree, if it has >=2
children then it's articulation. For non-root u, if it has child v with low[v] >= disc[u], u is
articulation. Complexity O(V+E). Correctness: low[v] >= disc[u] implies no back-edge from v's
subtree to ancestors of u, so removing u disconnects v's subtree.

16. Explain the reduction proving that 3-Coloring is NP-complete.


Answer: 3-Coloring is in NP. Reduce from 3-SAT: construct a graph where variable gadgets
enforce true/false via two colors and clause gadgets connect to literals ensuring at least one
true literal maps to a compatible color; standard constructions (triangle for base colors) encode
clauses. Complexity: poly-time. Therefore 3-Coloring is NP-complete.

17. Is universality (grammar generates all strings) decidable for context-free


grammars? Explain.
Answer: Universality for context-free grammars is undecidable in general. For regular languages
(finite automata) universality is decidable. For unrestricted (recursively enumerable) grammars,
many properties are undecidable. The universality undecidability for CFGs follows from
reductions from PCP or other undecidable problems (this is a subtle topic—be precise in
interviews).

18. Prove Savitch's theorem: NSPACE(s(n)) subseteq DSPACE(s(n)^2).


Answer: Savitch's theorem uses a recursive algorithm to decide reachability in the configuration
graph using O(s(n)^2) space. Define REACH(u,v,t): whether v reachable from u within t steps.
Use divide-and-conquer: check a mid configuration m and test REACH(u,m,t/2) and
REACH(m,v,t/2) recursively. Space needed is O(s(n) log t) = O(s(n)^2) for t <= 2^{O(s(n))}. Hence
NSPACE(s(n)) subseteq DSPACE(s(n)^2).

19. Explain fixed-parameter tractability (FPT) and give an example algorithm for
k-Vertex Cover with runtime O(2^k * poly(n)).
Answer: FPT problems have algorithms running in f(k) * poly(n) where k is a parameter. For
Vertex Cover: branching algorithm picks an edge (u,v); either u in cover or v in cover => T(k) <= 2
T(k-1) => O(2^k). With kernelization you can reduce to O(k^2) vertices and then run exact or
branching algorithm. Thus overall O(2^k * poly(n)).

20. Describe high-level ideas for proving lower bounds in the cell-probe model
for static predecessor/membership problems.
Answer: Cell-probe model measures memory cells probed. Lower bounds use information-
theoretic and communication complexity techniques — reduce set membership/predecessor to
hard communication tasks, or use chronogram/cell-sampling arguments to show any scheme
with t probes requires large space. For predecessor, Pǎtraşcu and Thorup showed trade-offs
between space and probe time; proofs use hard distributions and encoding arguments.

You might also like