Practice: DSA Mock Coding Round
75 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)A real coding round gives you three problems and an hour, and the interviewer scores how you talk as much as what you type. This mock puts the whole module together: you solve under a timer, score yourself, then face the follow-ups.
How to run this mock round
A mock coding round is a timed practice interview: three problems solved aloud with the full routine, then scored by yourself with a rubric.
Simulate the real thing. Use a timer, speak aloud (record yourself on your phone if you can), and work in your IDE with a blank template or on paper. The three problems are Practice items 1 to 3; do not open the solutions until the time for that problem is up.
After each problem, score yourself with the rubric below, then try its follow-up variants (Practice 4 to 8). Read the warm-up in the Learn examples before you start the timer. Revise: The medium pattern set (opens in a new tab)
- Problem 1, easy, 15 minutes: Rotate Array (Practice 1).
- Problem 2, medium, 20 minutes: Product of Array Except Self (Practice 2).
- Problem 3, medium, 20 minutes: Merge Intervals (Practice 3).
- Then follow-ups: Practice 4 to 8, about 10 minutes each.
The model transcript: clarify, brute force, optimize, code, dry run, complexity
A model transcript is the fixed spoken routine of a strong round: clarify, brute force, optimize, code, dry run, then complexity and edge cases.
A strong round sounds the same every time. Example 1 solves Find Pivot Index and keeps the spoken routine as comments, so the code and the narration match.
Example 1 · Warm-up: Find Pivot Index, from brute force to O(n)
public class PivotIndexDemo { // Clarify: leftmost index whose left sum equals right sum (element excluded), else -1. // Brute force: O(n²) - for each i, add up both sides. static int pivotBrute(int[] a) { for (int i = 0; i < a.length; i++) { int left = 0, right = 0; for (int j = 0; j < i; j++) left += a[j]; for (int j = i + 1; j < a.length; j++) right += a[j]; if (left == right) return i; } return -1; }
// Optimized: O(n) time, O(1) space - right = total - left - a[i]. static int pivot(int[] a) { int total = 0; for (int x : a) total += x; int left = 0; for (int i = 0; i < a.length; i++) { if (left == total - left - a[i]) return i; left += a[i]; } return -1; }
public static void main(String[] args) { int[][] tests = {{1, 7, 3, 6, 5, 6}, {1, 2, 3}, {2, 1, -1}, {}}; for (int[] t : tests) { System.out.println("brute " + pivotBrute(t) + ", fast " + pivot(t)); } }}brute 3, fast 3 brute -1, fast -1 brute 0, fast 0 brute -1, fast -1
What this code does
- The comments are the spoken transcript: clarification first, then each solution with its complexity, so the code and the narration match.
- The tests include the agreed edge cases: no pivot, pivot at index 0 with negatives, and an empty array.
Find Pivot Index ("the index where the sum on the left equals the sum on the right"), said aloud:
- Clarify: "Return the leftmost such index, or -1? Can values be negative? Is the element itself excluded from both sides?"
- Brute force: "For each index, add up both sides: O(n²) time, O(1) space."
- Optimize: "The total is fixed, so I keep a running left sum: right = total - left - a[i]. That is O(n) time, O(1) space."
- Code in small pieces, then dry run
[1, 7, 3, 6, 5, 6]: total 28; at index 3, left is 11 and right is 28 - 11 - 6 = 11, so the answer is 3. - Complexity and edge cases: "O(n) time, O(1) extra space; an empty array returns -1;
[2, 1, -1]returns 0 because the left side of index 0 is empty."
Predict the output
Dry run the pivot
Run the optimized solution by hand and predict the pivot index.
Round 1 of 2
What index does it print?
Self-score rubric for communication and correctness
A self-score rubric is a 10-point checklist you apply after each problem: understanding 2, approach 2, code 3, verification and complexity 3.
Scoring honestly is how a mock round teaches you. Example 2 checks the fast pivot solution against the brute force on 2,000 random arrays, the test-oracle habit from the earlier lesson.
Example 2 · Checking the fast answer against the brute force
import java.util.Random;
public class PivotOracleDemo { static int pivotBrute(int[] a) { for (int i = 0; i < a.length; i++) { int left = 0, right = 0; for (int j = 0; j < i; j++) left += a[j]; for (int j = i + 1; j < a.length; j++) right += a[j]; if (left == right) return i; } return -1; }
static int pivot(int[] a) { int total = 0; for (int x : a) total += x; int left = 0; for (int i = 0; i < a.length; i++) { if (left == total - left - a[i]) return i; left += a[i]; } return -1; }
public static void main(String[] args) { Random random = new Random(7); int agree = 0; int withPivot = 0; for (int t = 0; t < 2000; t++) { int[] a = new int[random.nextInt(8)]; for (int i = 0; i < a.length; i++) a[i] = random.nextInt(7) - 3; int expected = pivotBrute(a); if (expected == pivot(a)) agree++; if (expected != -1) withPivot++; } System.out.println("agree on " + agree + " of 2000 random arrays"); System.out.println("arrays that had a pivot: " + (withPivot > 0 ? "some" : "none")); }}agree on 2000 of 2000 random arrays arrays that had a pivot: some
What this code does
- Small random arrays with negatives and zeros cover cases you would not think of; the brute force is the trusted answer.
- Checking that some arrays really had a pivot makes sure the test is not passing only because every answer was -1.
Score each problem out of 10, right after you finish it. Interviewers weigh communication almost as much as the code, so both halves count:
- Understanding (2): restated the problem, asked the questions that matter, agreed examples and edge cases.
- Approach (2): stated a brute force with complexity, then a better approach with its trade-off, before coding.
- Code (3): compiles, correct on the examples and edge cases, readable names, no dead code.
- Verification and complexity (3): dry run or test calls, found or ruled out bugs, stated time and space with reasons.
Follow-up variants interviewers add
A follow-up variant is the same problem with one condition changed: the input, the output, a limit such as O(1) extra space, or the operation.
A follow-up changes one condition to test whether you understood your own solution. Example 3 gives the warm-up two: all pivot indices, and an array that keeps changing.
Example 3 · A follow-up on the warm-up: all pivots, and a changing array
import java.util.ArrayList;import java.util.List;
public class PivotFollowUpDemo { // Follow-up 1: return ALL pivot indices - same single pass, collect instead of return. static List<Integer> allPivots(int[] a) { int total = 0; for (int x : a) total += x; List<Integer> result = new ArrayList<>(); int left = 0; for (int i = 0; i < a.length; i++) { if (left == total - left - a[i]) result.add(i); left += a[i]; } return result; }
// Follow-up 2: values change often - keep the total up to date in O(1) per update, // so each pivot query stays one O(n) pass with no recomputation of the total. static int total;
static void update(int[] a, int i, int value) { total += value - a[i]; a[i] = value; }
static int pivot(int[] a) { int left = 0; for (int i = 0; i < a.length; i++) { if (left == total - left - a[i]) return i; left += a[i]; } return -1; }
public static void main(String[] args) { System.out.println("all pivots of [0, 0, 0]: " + allPivots(new int[] {0, 0, 0})); int[] a = {1, 7, 3, 6, 5, 6}; for (int x : a) total += x; System.out.println("pivot before update: " + pivot(a)); update(a, 0, 10); System.out.println("pivot after a[0] = 10: " + pivot(a)); }}all pivots of [0, 0, 0]: [0, 1, 2] pivot before update: 3 pivot after a[0] = 10: 2
What this code does
- The first follow-up changes the output (all answers), so the same pass collects indices instead of returning early.
- The second changes the setting (frequent updates): maintaining the total avoids an extra pass per query. With [10, 7, 3, 6, 5, 6], index 2 has 17 on both sides.
Expect at least one follow-up per problem and answer it by naming what changes: the input (unsorted, negatives, zeros, k larger than n), the output (all answers instead of one), the limits (O(1) extra space, a data stream) or the operation (insert instead of merge).
Say first whether your current approach still works and why, then adapt it. The warm-up follow-ups in the examples and Practice 4 to 8 give you the common ones for this round's problems.
Order of execution
Answer a follow-up
Put the steps of a strong answer to "now what if the array changes?" in order.
Next step
Check your understanding
12 quick questions