Practice
1. You are given a string and a list of index pairs where you can swap characters any number of times. The goal is to find the lexicographically smallest string possible after performing any swaps allowed by these pairs. Which algorithmic approach best guarantees an optimal solution for this problem?
easy
Solution
Step 1: Understand problem constraints
The problem allows unlimited swaps between indices connected by pairs, so indices form connected components.Step 2: Identify approach that groups indices
Union-Find efficiently groups indices into connected components, enabling sorting characters within each group to get the smallest lexicographic order.Final Answer:
Option B -> Option BQuick Check:
Union-Find groups indices correctly, sorting within groups yields optimal string [OK]
Hint: Group indices first, then sort characters within groups [OK]
Common Mistakes:
- Assuming greedy swaps suffice
- Trying DP over all permutations
- Using BFS to explore all strings exhaustively
2. Consider the following code snippet implementing the optimal approach for the smallest string with swaps problem. Given input string
s = "dcab" and pairs [[0,3],[1,2]], what is the final returned string?easy
Solution
Step 1: Perform unions on pairs
Union(0,3) connects indices 0 and 3; Union(1,2) connects indices 1 and 2.Step 2: Group indices and sort characters
Group 1: indices [0,3] with chars ['d','b'] sorted to ['b','d']; Group 2: indices [1,2] with chars ['c','a'] sorted to ['a','c'].Step 3: Assign sorted chars back to sorted indices
Assign 'b' to index 0, 'd' to index 3; assign 'a' to index 1, 'c' to index 2.Final Answer:
Option C -> Option CQuick Check:
Resulting string is "acbd" after swaps [OK]
Hint: Sort characters within connected index groups [OK]
Common Mistakes:
- Swapping characters without sorting indices
- Mixing up group indices and characters
3. What is the time complexity of the optimal union-find solution for the Most Stones Removed with Same Row or Column problem, given n stones?
medium
Solution
Step 1: Analyze union-find operations
Each union/find operation runs in nearly O(1) amortized time, specifically O(α(n)) where α is inverse Ackermann.Step 2: Count total operations
We perform at most 2n find operations and n union operations, total O(n α(n)).Final Answer:
Option A -> Option AQuick Check:
Union-find with path compression and rank -> O(n α(n)) [OK]
Hint: Union-find with path compression -> near-linear time [OK]
Common Mistakes:
- Assuming O(n^2) from brute force pairwise checks
- Confusing sorting complexity with union-find
- Ignoring inverse Ackermann function in complexity
4. What is the time complexity of the optimal Union-Find algorithm with path compression and union by rank when finding the redundant connection in a graph with
n edges?medium
Solution
Step 1: Identify number of union/find operations
There are n edges, each triggers at most one union and two finds.Step 2: Analyze complexity of find with path compression and union by rank
Each find/union operation runs in amortized O(α(n)) time, where α(n) is inverse Ackermann function, which grows extremely slowly.Final Answer:
Option B -> Option BQuick Check:
Union-Find with optimizations runs in near-linear time O(n * α(n)) [OK]
Hint: Inverse Ackermann function α(n) is almost constant [OK]
Common Mistakes:
- Assuming O(n²) due to nested loops
- Confusing sorting complexity with union-find
- Ignoring path compression benefits
5. Suppose the problem is modified so that edges can be reused multiple times (i.e., multi-edges allowed), and we want to remove the maximum number of edges while keeping the graph traversable for both Alice and Bob. Which modification to the optimal union-find approach is necessary?
hard
Solution
Step 1: Understand multi-edge impact
Multi-edges mean multiple edges connect same nodes; union-find alone merges sets once, ignoring multiplicity.Step 2: Modify union-find approach
Need to track edge counts per connection and only remove edges that do not break connectivity, requiring a multigraph-aware union-find or additional bookkeeping.Final Answer:
Option A -> Option AQuick Check:
Original union-find ignores multiplicity -> must track edge counts [OK]
Hint: Union-find merges sets once; multi-edges require counting edges [OK]
Common Mistakes:
- Assuming union-find handles multi-edges without change
- Ignoring edge multiplicity
- Switching to unrelated algorithms unnecessarily
