Skip to lesson

Mock online coding assessment

An online coding assessment is the first filter in many placement drives: two problems, one timer, and hidden tests that reward only programs that are correct, fast and print exactly the expected output. Set a 60-minute timer and try both problems before you read any solution.

Mock rules: two problems, 60 minutes, Java 17, stdin to stdout

An online coding assessment (OA) is a timed test where you solve a few problems in an online editor, your program reads input from standard input, and hidden test cases decide your score.

Standard input (stdin) is where the platform sends the test input; standard output (stdout) is what it reads back, character for character. This reader handles numbers on any number of lines, faster than Scanner.

Java
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tok = new StringTokenizer("");
static String next() throws IOException {    while (!tok.hasMoreTokens()) {        tok = new StringTokenizer(in.readLine());    }    return tok.nextToken();}

Rules for this mock: 60 minutes for both problems, no internet search, and print exactly what the output format says (no extra text such as "Answer:"). Many platforms expect a class named Main and Java 17 or newer; check the real instructions, since platforms differ.

A 60-minute online coding assessment split into four parts: 5 minutes to read both problems and the limits, 20 minutes for the easier problem, 30 minutes for the harder problem, and 5 minutes to run the checklist and submit.

Order of execution

Plan your 60 minutes

Put the four parts of a 60-minute assessment in the order you do them.

Problem 1: the longest run within a cloud budget

A sliding window is a range [left, right] over an array that grows on the right and shrinks on the left, so every element enters and leaves the window at most once.

Problem 1 asks for the longest run of days whose total cost stays within a budget. Try it for 20 minutes first, then compare with the model solution.

Example 1 · Problem 1 statement

Text
Problem 1: the longest run within a budgetA startup records its cloud spend for n days in a row: spend[1..n] dollars.Find the longest run of consecutive days whose total is at most B dollars.Print its length, or 0 if no single day fits.Input:  line 1 has n and B; line 2 has n numbersLimits: 1 <= n <= 2 x 10^5, 1 <= spend[i] <= 10^9, 0 <= B <= 10^14Sample input:7 500120 80 300 50 60 90 400
Expected output for the sample input
4

What this code does

  • Read the limits first: n = 2 × 10⁵ rules out O(n²), and a total of up to 2 × 10¹⁴ does not fit in int.
  • In the sample, days 2 to 5 cost 80 + 300 + 50 + 60 = 490, which is at most 500, and no run of five days fits.
  • Try the problem yourself before you read the model solution below.

Example 2 · Problem 1 model solution (sliding window)

Main.javaJava
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;
public class Main {    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    static StringTokenizer tok = new StringTokenizer("");
    static String next() throws IOException {        while (!tok.hasMoreTokens()) {            tok = new StringTokenizer(in.readLine());        }        return tok.nextToken();    }
    public static void main(String[] args) throws IOException {        int n = Integer.parseInt(next());        long budget = Long.parseLong(next());        long[] spend = new long[n];        for (int i = 0; i < n; i++) {            spend[i] = Long.parseLong(next());        }
        long sum = 0;      // total of the current window spend[left..right]        int left = 0;        int best = 0;        for (int right = 0; right < n; right++) {            sum += spend[right];            while (sum > budget) {   // too costly: drop days from the left                sum -= spend[left];                left++;            }            best = Math.max(best, right - left + 1);        }        System.out.println(best);    }}
Sample output (for the sample input 7 500 / 120 80 300 50 60 90 400)
4

What this code does

  • The window spend[left..right] always has a total of at most B after the while loop, so its length is a valid answer.
  • Each index moves forward only, so the two loops together do at most 2n steps: O(n).
  • sum and budget are long; with int, a total like 3 × 10⁹ would wrap around to a negative number.

The model solution adds the new day on the right and, while the total is above B, drops days from the left. Time is O(n), because each day enters and leaves the window once; extra space is O(n) for the array.

Totals can reach 2 × 10¹⁴, far above the int limit of about 2.1 × 10⁹, so use long. The window works only because every spend is positive: adding a day never lowers the total. Revise: Variable-size window (opens in a new tab).

Fix the error

Fix the compile error

Read the real compiler error, then pick the line that fixes it.

Round 1 of 2

B can be as large as 10^14. Read it into the budget variable.

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int budget = in.nextLong();
System.out.println(n + " " + budget);
}
}

Error

Main.java:7: error: incompatible types: possible lossy conversion from long to int
        int budget = in.nextLong();
                                ^

Problem 2: the minimum number of classrooms

The minimum rooms problem asks for the fewest rooms that fit all lectures. The answer is the largest number of lectures running at the same moment.

Problem 2 is the classic "Minimum Platforms" problem. A room frees up when its lecture ends, so back-to-back lectures can share one room.

Example 3 · Problem 2 statement

Text
Problem 2: the minimum number of classroomsA college timetable has n lectures, each with a start and an end time.A room is free for a new lecture once the previous lecture's end time isreached: one ending at 10 and one starting at 10 can share a room.Print the minimum number of rooms needed.Input:  line 1 has n; then n lines of "start end"Limits: 1 <= n <= 10^5, 0 <= start < end <= 10^9Sample input:69 109 129 1110 1111 1312 13
Expected output for the sample input
3

What this code does

  • The answer is the largest number of lectures running at the same moment.
  • In the sample, three lectures start at 9 and overlap, so 3 rooms are needed; later lectures reuse rooms that were freed.
  • The tie rule in the statement (end equals start can share) decides several hidden tests.

Example 4 · Problem 2 model solution (sorted starts and ends)

Main.javaJava
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;import java.util.StringTokenizer;
public class Main {    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    static StringTokenizer tok = new StringTokenizer("");
    static String next() throws IOException {        while (!tok.hasMoreTokens()) {            tok = new StringTokenizer(in.readLine());        }        return tok.nextToken();    }
    public static void main(String[] args) throws IOException {        int n = Integer.parseInt(next());        int[] start = new int[n];        int[] end = new int[n];        for (int i = 0; i < n; i++) {            start[i] = Integer.parseInt(next());            end[i] = Integer.parseInt(next());        }        Arrays.sort(start);        Arrays.sort(end);
        int rooms = 0;        int best = 0;        int j = 0;                       // next lecture to finish        for (int i = 0; i < n; i++) {    // lectures in order of start time            while (j < n && end[j] <= start[i]) {                rooms--;                 // that lecture is over: its room is free                j++;            }            rooms++;            best = Math.max(best, rooms);        }        System.out.println(best);    }}
Sample output (for the six sample lectures)
3

What this code does

  • Sorting starts and ends separately works because only the count of running lectures matters, not which lecture is which.
  • end[j] <= start[i] frees a room for a lecture starting exactly when another ends; using < here fails the tie tests.
  • Sorting costs O(n log n); the walk is O(n), since j only moves forward.

The model solution sorts the start times and the end times separately, then walks through the starts in order. Before each start it frees every room whose lecture has ended (end ≤ start), then takes a room. Time is O(n log n) for sorting and O(n) for the walk; extra space is O(n).

The tie rule (end ≤ start frees the room) is the most common hidden-test trap. Revise: Meeting rooms (opens in a new tab).

Predict the output

The tie rule

Two lectures end and start at the same minute. Does one room do?

Round 1 of 2

Lectures (9,10), (10,11), (10,12). The loop frees a room when end <= start. What is printed?

import java.util.Arrays;
public class RoomsCheck {
public static void main(String[] args) {
int[] start = {9, 10, 10};
int[] end = {10, 11, 12};
Arrays.sort(start);
Arrays.sort(end);
int rooms = 0, best = 0, j = 0;
for (int i = 0; i < start.length; i++) {
while (j < end.length && end[j] <= start[i]) {
rooms--;
j++;
}
rooms++;
best = Math.max(best, rooms);
}
System.out.println(best);
}
}

Sample and hidden-style tests, and what partial scores tell you

Hidden tests are the many tests a platform runs after you submit, usually including edge cases and the largest inputs, so passing the sample tests proves very little.

Read your verdict like a report: TLE on the largest tests points to a slow algorithm, WA on large tests to overflow, WA on small tests to a logic or edge-case bug.

Example 5 · Problem 1 brute force: correct but too slow

Main.javaJava
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.StringTokenizer;
public class Main {    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));    static StringTokenizer tok = new StringTokenizer("");
    static String next() throws IOException {        while (!tok.hasMoreTokens()) {            tok = new StringTokenizer(in.readLine());        }        return tok.nextToken();    }
    public static void main(String[] args) throws IOException {        int n = Integer.parseInt(next());        long budget = Long.parseLong(next());        long[] spend = new long[n];        for (int i = 0; i < n; i++) {            spend[i] = Long.parseLong(next());        }
        int best = 0;        for (int i = 0; i < n; i++) {          // every start day            long sum = 0;            for (int j = i; j < n; j++) {      // every end day: O(n^2) in total                sum += spend[j];                if (sum > budget) {                    break;                }                best = Math.max(best, j - i + 1);            }        }        System.out.println(best);    }}
Sample output (for the sample input; on n = 2 x 10^5 it runs out of time)
4

What this code does

  • It tries every start day and extends to the right until the total passes B: correct, and a good first submission if you are stuck.
  • For n = 2 × 10⁵ it can take about 2 × 10¹⁰ steps, far beyond the usual one or two seconds, so the largest hidden tests show TLE.
  • A partial score such as "6 of 10 tests passed, the rest TLE" is exactly this pattern.

Example 6 · A stress test: brute force versus sliding window

StressTestDemo.javaJava
import java.util.Random;
public class StressTestDemo {    static int window(long[] spend, long budget) {        long sum = 0;        int left = 0, best = 0;        for (int right = 0; right < spend.length; right++) {            sum += spend[right];            while (sum > budget) {                sum -= spend[left++];            }            best = Math.max(best, right - left + 1);        }        return best;    }
    static int brute(long[] spend, long budget) {        int best = 0;        for (int i = 0; i < spend.length; i++) {            long sum = 0;            for (int j = i; j < spend.length; j++) {                sum += spend[j];                if (sum > budget) {                    break;                }                best = Math.max(best, j - i + 1);            }        }        return best;    }
    public static void main(String[] args) {        Random rnd = new Random(42);   // fixed seed: the same tests every run        int tests = 300;        for (int t = 1; t <= tests; t++) {            int n = 1 + rnd.nextInt(8);            long[] spend = new long[n];            for (int i = 0; i < n; i++) {                spend[i] = 1 + rnd.nextInt(20);            }            long budget = rnd.nextInt(60);            if (window(spend, budget) != brute(spend, budget)) {                System.out.println("Mismatch on test " + t);                return;            }        }        System.out.println("Sample answer: " + window(new long[] {120, 80, 300, 50, 60, 90, 400}, 500));        System.out.println(tests + " random tests: all match");    }}
Output
Sample answer: 4
300 random tests: all match

What this code does

  • Small random inputs (n up to 8, values up to 20) cover edge cases you might not think of, such as B = 0 or every day too costly.
  • If the two methods ever disagree, the program prints the test number, and you can print that input to debug it.
  • A fixed seed (new Random(42)) makes the run repeatable; remove the seed to try new tests each time.

Sample tests are the examples in the problem. A partial score counts the hidden tests you passed, often with a verdict such as Wrong Answer (WA) or Time Limit Exceeded (TLE). A stress test compares your fast solution with a simple brute force on many small random inputs and finds such bugs before you submit.

Edge cases to try:

  • Problem 1: n = 1; B = 0; one day costs more than B; all days fit; values near 10⁹.
  • Problem 2: n = 1; all lectures at the same time; back-to-back lectures; no overlaps.
  • Largest tests: n at the maximum, to catch O(n²) code and overflow.

Find the bug

Read the verdict

Each piece of code matches a verdict. Tap the line that causes it.

Small tests pass; the largest tests give Wrong Answer.

Round 1 of 2

Coding-round checklist

A coding-round checklist is the short list you run through before every submit, to catch the mistakes that cost the most marks: extra output, wrong class name, overflow and missed edge cases.

Run this list for about one minute per problem, just before you press submit.

Text
[ ] Class name and input/output format exactly as stated[ ] No debug prints and no prompts like "Enter n"[ ] long wherever a sum or product can pass about 2.1 x 10^9[ ] Complexity fits the limits (n = 2 x 10^5 needs O(n) or O(n log n))[ ] Tried n = 1, all equal values, the "no answer" case, ties[ ] Ran the sample input once more after the last change

Next step

Check your understanding

12 quick questions