Coding-round time management and common problem patterns
40 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A coding round has three problems and 90 minutes. Most failed rounds are not about missing knowledge: the candidate spends 60 minutes on one problem, never submits a brute force and never tests an edge case.
Splitting 60 to 90 minutes across problems
A time plan for a coding round decides, before you write code, how long each problem gets. You spend the first minutes reading every problem and then rank them by how quickly you can solve them.
The example below turns a 90-minute round with three problems into a schedule: read first, easiest problem first, hardest last.
Example 1 · A 90-minute plan, easiest first
import java.util.Arrays;import java.util.Comparator;
record Problem(String name, int estimateMinutes) {}
public class RoundPlanDemo { static String clock(int minutes) { return String.format("%02d:%02d", minutes / 60, minutes % 60); }
public static void main(String[] args) { Problem[] problems = { new Problem("C (hard)", 40), new Problem("A (easy)", 15), new Problem("B (medium)", 25) }; Arrays.sort(problems, Comparator.comparingInt(Problem::estimateMinutes));
int t = 0; System.out.println(clock(t) + "-" + clock(t + 8) + " Read all three problems"); t += 8; for (Problem p : problems) { System.out.println(clock(t) + "-" + clock(t + p.estimateMinutes()) + " Problem " + p.name()); t += p.estimateMinutes(); } System.out.println(clock(t) + "-" + clock(90) + " Buffer: check final submissions"); }}00:00-00:08 Read all three problems 00:08-00:23 Problem A (easy) 00:23-00:48 Problem B (medium) 00:48-01:28 Problem C (hard) 01:28-01:30 Buffer: check final submissions
What this code does
- The problems appear as C, A, B on the screen, but sorting by estimated time gives the order A, B, C. The easy one is submitted within 23 minutes.
- Estimates are guesses. If B is still not working at 00:48, submit its brute force and move to C anyway.
Solve the easiest first and submit it. That locks in marks and calms you down. Give the hardest problem whatever time is left.
- Read all problems first: 5 to 10 minutes.
- If stuck for 10 to 15 minutes, submit a brute force and move on.
- Last 2 to 3 minutes: check every problem has its best version submitted.
Predict the output
Predict the plan
Read the program and pick what it prints.
Common patterns in fresher coding rounds
A prefix sum array stores running totals, so the sum of any range of elements is one subtraction: prefix[r] - prefix[l - 1]. After one O(n) build, every range query takes O(1).
Most fresher rounds lean on a few patterns. The example below shows one of them, prefix sums, answering range totals in O(1).
Example 2 · Prefix sums: range totals in O(1)
public class PrefixSumDemo { public static void main(String[] args) { int[] spend = { 120, 80, 300, 50, 200, 150, 90 }; // UPI spend per day, days 1 to 7 long[] prefix = new long[spend.length + 1]; for (int i = 0; i < spend.length; i++) { prefix[i + 1] = prefix[i] + spend[i]; }
int[][] queries = { { 2, 5 }, { 1, 7 }, { 4, 4 } }; for (int[] q : queries) { long total = prefix[q[1]] - prefix[q[0] - 1]; System.out.println("Days " + q[0] + " to " + q[1] + ": " + total); } }}Days 2 to 5: 630 Days 1 to 7: 990 Days 4 to 4: 50
What this code does
prefix[i]holds the total of the first i days, so the total of days l to r isprefix[r] - prefix[l - 1]: one subtraction per query.- With 10⁵ days and 10⁵ queries, adding up each range again would take about 10¹⁰ steps; prefix sums need about 2 × 10⁵.
A pattern is a known way to solve a whole family of problems. Learn to spot it from the wording:
- "How many times", "duplicate", "first unique" → hashing (count or look up in a
HashMaporHashSet). - "Sum of elements from day l to day r", many queries → prefix sums.
- "Pair with sum", sorted input → two pointers or hashing.
- "Minimum number of coins or rooms" → sort, then greedy (always take the best option now).
Revise: Prefix sum arrays (opens in a new tab) and HashMap and HashSet patterns (opens in a new tab)
Find the bug
Find the pattern bug
Each snippet uses a pattern from this lesson. Tap the wrong line.
Build prefix sums for the marks. The totals come out as zeros.
Round 1 of 3Brute force first, then optimise
A brute force solution tries every possibility directly, such as checking every pair. It is often too slow for the largest tests, but it is quick to write and usually right.
The example below finds the first repeated value twice: with two nested loops (brute force), then with a HashSet.
Example 3 · First repeated value: brute force and HashSet
import java.util.HashSet;import java.util.Set;
public class FirstRepeatDemo { static int comparisons = 0;
static int bruteForce(int[] a) { // O(n^2) for (int j = 1; j < a.length; j++) { for (int i = 0; i < j; i++) { comparisons++; if (a[i] == a[j]) return a[j]; } } return -1; }
static int withSet(int[] a) { // O(n) Set<Integer> seen = new HashSet<>(); for (int v : a) { if (!seen.add(v)) return v; // add returns false if v was already there } return -1; }
public static void main(String[] args) { int[] a = { 4, 9, 2, 7, 9, 4, 1 }; System.out.println("Brute force: " + bruteForce(a) + " after " + comparisons + " comparisons"); System.out.println("HashSet: " + withSet(a)); }}Brute force: 9 after 8 comparisons HashSet: 9
What this code does
- Both return 9: it is the first value, scanning left to right, that was already seen. The brute force checks every earlier element for each position, which is fine for small n and quick to write.
seen.add(v)returnsfalsewhenvis already in the set, so the HashSet version finds the repeat in one pass. Submit the brute force first if the fast idea is not clear yet.
With partial scoring a brute force can earn marks on the small hidden tests, so write it, submit it, then look for the faster idea. A brute force also helps later: it is the reference you compare the fast version against.
Fix the error
Fix the compile errors
Read the real compiler error, then pick the line that fixes it.
Round 1 of 2
Print the first repeated value with a HashSet.
Error
FirstDup.java:6: error: cannot find symbol
Set<Integer> seen = new HashSet<>();
^
symbol: class HashSetTesting before you submit
An edge case is an input at the limits of the constraints: an empty string, a single element, all values equal, the largest n, the largest values. Most wrong answers on hidden tests come from these.
The example below runs the edge cases through a fast solution and stress-tests it against a brute force on many small random inputs.
Example 4 · Edge cases and a stress test
import java.util.Arrays;import java.util.HashSet;import java.util.Random;import java.util.Set;
public class StressTestDemo { static int bruteForce(int[] a) { for (int j = 1; j < a.length; j++) { for (int i = 0; i < j; i++) { if (a[i] == a[j]) return a[j]; } } return -1; }
static int withSet(int[] a) { Set<Integer> seen = new HashSet<>(); for (int v : a) { if (!seen.add(v)) return v; } return -1; }
public static void main(String[] args) { int[][] edges = { {}, { 5 }, { 3, 3 }, { 1, 2, 3 }, { Integer.MAX_VALUE, Integer.MAX_VALUE } }; for (int[] e : edges) { System.out.println(Arrays.toString(e) + " -> " + withSet(e)); }
Random random = new Random(7); int agree = 0; for (int t = 0; t < 200; t++) { int[] a = new int[1 + random.nextInt(8)]; for (int i = 0; i < a.length; i++) a[i] = random.nextInt(10); if (bruteForce(a) == withSet(a)) agree++; } System.out.println("Random tests: " + agree + " of 200 agree"); }}[] -> -1 [5] -> -1 [3, 3] -> 3 [1, 2, 3] -> -1 [2147483647, 2147483647] -> 2147483647 Random tests: 200 of 200 agree
What this code does
- The edge cases cover an empty array, one element, a repeat right at the start, no repeat at all and the largest
intvalue. Each prints what the problem expects (-1 means no repeat). - The stress test builds 200 small random arrays with a fixed seed and checks that both solutions agree. If one ever differed, printing that array would show the bug at once.
A stress test compares your fast solution with the brute force on many small random inputs. If they ever disagree, you have found a bug and the input that shows it. Use a fixed seed such as new Random(7) so the same inputs come back each run.
Revise: The edge case checklist (opens in a new tab) and Stress testing against brute force (opens in a new tab)
Order of execution
Run a stress test
Put the steps of a stress test in order.
Worked example: most frequent roll number
A frequency count stores how many times each value appears, usually in a HashMap<Integer, Integer>, in one pass over the data. Looking up and updating a count takes O(1) on average.
Problem: the library logs the roll number of every visit. Print the roll number with the most visits and its count; ties go to the smallest roll number. Constraints: n ≤ 10⁵.
Example 5 · Most frequent roll number: O(n²) and O(n)
import java.util.HashMap;import java.util.Map;
public class FrequencyCountDemo { static int[] bruteForce(int[] rolls) { // O(n^2) int best = -1, bestCount = 0; for (int r : rolls) { int count = 0; for (int x : rolls) if (x == r) count++; if (count > bestCount || (count == bestCount && r < best)) { best = r; bestCount = count; } } return new int[] { best, bestCount }; }
static int[] withMap(int[] rolls) { // O(n) Map<Integer, Integer> freq = new HashMap<>(); for (int r : rolls) freq.merge(r, 1, Integer::sum); int best = -1, bestCount = 0; for (Map.Entry<Integer, Integer> e : freq.entrySet()) { int r = e.getKey(), count = e.getValue(); if (count > bestCount || (count == bestCount && r < best)) { best = r; bestCount = count; } } return new int[] { best, bestCount }; }
public static void main(String[] args) { int[] rolls = { 104, 101, 104, 230, 101, 104, 230, 101 }; int[] slow = bruteForce(rolls); int[] fast = withMap(rolls); System.out.println("Brute force: roll " + slow[0] + " (" + slow[1] + " visits)"); System.out.println("HashMap: roll " + fast[0] + " (" + fast[1] + " visits)"); }}Brute force: roll 101 (3 visits) HashMap: roll 101 (3 visits)
What this code does
- Rolls 101 and 104 both have 3 visits, so the tie rule picks the smaller one, 101. The condition
count == bestCount && r < besthandles the tie in both versions, so the answer does not depend on the map's order. freq.merge(r, 1, Integer::sum)adds 1 to the count ofr, starting from 1 the first time. The map version makes one pass to count and one pass over the distinct rolls: O(n) overall.
The brute force counts each value by scanning the whole array again: O(n²), about 10¹⁰ steps for n = 10⁵, far too slow. The frequency count does one pass to count, then one pass over the map to find the winner using the tie rule: O(n).
Revise: Counting and grouping with maps (opens in a new tab)
Predict the output
Predict the winner
Read each program and pick what it prints.
Round 1 of 2
Next step
Check your understanding
12 quick questions