HackerRank Tips & Strategy
This is not about solving logic. Most students who fail HSE can solve the problem — they fail because of how HackerRank works. Wrong output format, leftover newlines, extra spaces, debug prints left in code. This page teaches you the platform itself.
The HackerRank Platform
HackerRank is a coding assessment platform used by companies worldwide. TCS uses it for HSE and sometimes PRA coding sections. Here's how it works:
- Online code editor — you write code in the browser, no local IDE
- Language locked to Java for TCS HSE (other exams may allow more)
- Input comes from stdin —
System.inin Java - Output goes to stdout —
System.outin Java - Your output is compared character-by-character against expected output
- No partial credit per test case — each test case is pass or fail
- You cannot see hidden test case inputs — only pass/fail status
HSE Exam Format (HackerRank Software Engineering Assessment)
HSE stands for HackerRank Software Engineering Assessment. It is one of the toughest exams in TCS ILP.
| Detail | Info |
|---|---|
| Platform | HackerRank |
| Key Topics | REST APIs are known to be critical. Java coding is likely. |
| Type | Complete programs with proper I/O — not fill-in-the-blank |
| Format | Exact format may vary by batch — prepare broadly |
- REST APIs are very important — JSON parsing, HTTP methods, API consumption
- Java coding is likely — OOP, collections, string handling
- Exact number of problems, marks, and duration may vary by batch
- You write complete programs with proper input/output on HackerRank
How to Read HackerRank Questions
Most students start coding immediately. This is the biggest mistake. The problem statement contains critical details that determine whether your solution passes or fails.
Step-by-step approach:
- Read the full problem statement — top to bottom, don't skim
- Read the "Note" section — this is where they hide critical info like "case-insensitive comparison" or exact output format
- Study the Input Format section — understand what each line of input contains
- Study the Output Format section — copy the exact string format. Letter by letter.
- Trace through Sample Input/Output — manually work through the example. Make sure your understanding matches
- Check the Constraints section — input size tells you if brute force is OK or you need optimization
- Plan your approach in comments — write pseudocode before real code
What to look for in the problem statement:
| Look for... | Why it matters |
|---|---|
| "case-insensitive" | Use .equalsIgnoreCase() — almost every TCS problem needs this |
| Exact output string | "No employee found" is different from "No Employee Found" — copy exactly |
| "sorted by" / "order by" | You need to sort before printing — order matters |
| Return type: "null" or "not found" | You MUST handle the case where nothing matches |
| Decimal places | "Display up to 2 decimal places" → use String.format("%.2f", val) |
| Constraints: N ≤ 1000 | Brute force O(n²) is fine. If N ≤ 10⁶, you need O(n log n) |
I/O Handling — The #1 Killer
On HackerRank, your program reads from stdin and writes to stdout. There are no prompts, no GUI, no file I/O. Just raw input → processing → raw output.
The Golden Rules:
- Never print prompts — no "Enter a number:", no "Input:", nothing
- Never leave debug prints — every extra
System.out.println()fails test cases - Always trim input —
sc.nextLine().trim()removes trailing whitespace - Match output exactly — one extra space, one wrong capital letter = fail
Safe Input Pattern (recommended):
Scanner sc = new Scanner(System.in); // Reading an integer — SAFE way int n = Integer.parseInt(sc.nextLine().trim()); // Reading a string String name = sc.nextLine().trim(); // Reading a double double price = Double.parseDouble(sc.nextLine().trim()); // Reading multiple values on one line String[] parts = sc.nextLine().trim().split(" "); int a = Integer.parseInt(parts[0]); int b = Integer.parseInt(parts[1]);
nextLine().trim() for everything avoids the Scanner trap (explained below). It reads the entire line including the newline character, so there's nothing left in the buffer to cause problems. The .trim() removes any trailing whitespace that might exist in the input.
The Scanner Trap — Memorize This
This single bug causes more HSE failures than any other issue. If you understand nothing else on this page, understand this.
The Problem:
Scanner sc = new Scanner(System.in); int n = sc.nextInt(); // reads "3" but leaves "\n" in buffer String name = sc.nextLine(); // reads the leftover "\n" → EMPTY STRING! // name is now "" (empty), NOT the actual next line of input // Your entire program breaks from here
Why it happens:
When you type 3 and press Enter, the input buffer contains "3\n". nextInt() reads "3" but leaves the "\n" in the buffer. The next nextLine() reads that leftover "\n" and returns an empty string.
The Fix — Option 1 (consume the newline):
int n = sc.nextInt(); sc.nextLine(); // ← consume the leftover newline String name = sc.nextLine(); // NOW this reads the actual next line
The Fix — Option 2 (recommended — avoid nextInt entirely):
int n = Integer.parseInt(sc.nextLine().trim()); // reads full line, no leftover String name = sc.nextLine().trim(); // reads next line cleanly
nextLine() for everything and parse manually (recommended), or always add an extra sc.nextLine() after every nextInt() / nextDouble() call. There is no third option.
The same trap with nextDouble:
// DANGEROUS double price = sc.nextDouble(); // leaves "\n" String category = sc.nextLine(); // reads "" (empty) // SAFE double price = Double.parseDouble(sc.nextLine().trim()); String category = sc.nextLine().trim(); // works correctly
Exact Output Matching
HackerRank compares your output character by character against the expected output. Here's what kills submissions:
| Mistake | Example | Result |
|---|---|---|
| Extra trailing space | "Hello " vs "Hello" | FAIL |
| Wrong case | "no gadget found" vs "No Gadget Found" | FAIL |
| Extra blank line | Extra println() at end | FAIL |
| Debug print left in | "Debug: x=5" before output | FAIL |
| Wrong decimal places | "95.5" vs "95.50" | FAIL |
| Missing newline at end | Using print() instead of println() | Usually FAIL |
Output formatting patterns:
// Print a line with newline at end (use this for most output) System.out.println("Result: " + value); // Format decimals to exactly 2 places System.out.println(String.format("Average: %.2f", avg)); // If avg = 95.5 → prints "Average: 95.50" // Print object details in a specific format System.out.println(emp.getName() + " - " + emp.getDept() + " - " + emp.getSalary()); // When no result found — copy the EXACT string from the problem System.out.println("No employee found"); // EXACT match required
How HackerRank Test Cases Work
Every HackerRank problem has multiple test cases. Your program runs once per test case — fresh input each time.
Types of test cases:
| Type | Visible? | Purpose |
|---|---|---|
| Sample test cases (2-3) | Yes — you see input, expected output, and your output | Verify your basic understanding |
| Hidden test cases (5-12) | No — you only see pass/fail (lock icon) | Test edge cases, large inputs, boundary conditions |
Scoring:
- Each test case has a point value (not always equal)
- No partial credit per test case — pass completely or get 0
- Total score = sum of all passed test case points
- Sample test cases may carry 0 points — they're just for you to verify
Common Mistakes (and How to Avoid Them)
1. The Scanner nextLine trap
// WRONG — will silently break your program int n = sc.nextInt(); String name = sc.nextLine(); // reads empty string // RIGHT int n = Integer.parseInt(sc.nextLine().trim());
2. Debug prints left in code
// You wrote this while testing: System.out.println("DEBUG: array size = " + arr.length); // REMOVE before submitting! // This extra line WILL fail every single test case
3. Not handling the "not found" case
// You filtered and found nothing — what now? if (result == null) { System.out.println("No employee found"); // MUST print this } // If you don't handle null, you get NullPointerException → 0 marks
4. Using == for String comparison
// WRONG — compares memory addresses if (name == "Sales") { ... } // RIGHT — compares actual string content if (name.equals("Sales")) { ... } // BEST — case-insensitive (TCS problems almost always need this) if (name.equalsIgnoreCase("Sales")) { ... }
5. Integer overflow on sums
// WRONG — if salaries are large, int overflows silently int total = 0; for (Employee e : employees) total += e.getSalary(); // RIGHT — use long for sums long total = 0; for (Employee e : employees) total += e.getSalary();
6. Wrong data type
// Problem says "price" — could be 99.99 int price = ...; // WRONG — loses decimal part double price = ...; // RIGHT
7. Hardcoded values
// Testing with sample input and accidentally leaving: int n = 3; // WRONG — must READ from input // RIGHT int n = Integer.parseInt(sc.nextLine().trim());
Exam Day Strategy
General time management:
| Phase | What to do |
|---|---|
| First 5-10 min | Read ALL problems first. Start with whichever feels easiest. |
| Per problem | Plan → Code → Test with sample → Check edge cases → Remove debug prints |
| Last 5-10 min | Review all output formats, recheck edge cases, remove debug prints |
Step-by-step for a coding problem:
- Read the problem twice — don't touch the keyboard yet
- Identify the class structure — what attributes, what types
- Write the class first — private fields, constructor, getters
- Write the input reading code — use the safe
nextLine().trim()pattern - Write the filtering/processing logic
- Handle the "not found" case
- Copy the exact output format from the problem statement
- Run against sample test cases
- Mentally check edge cases — 0 matches, all matches, 1 element
- Remove all debug prints
- Submit
1. Write the class — that's easy marks
2. Write the input reading — more easy marks
3. Write the output format — even if your logic is wrong, correct I/O structure may pass some cases
Partial marks > 0 marks.
What NOT To Do
- Don't start coding without reading the full problem — you'll miss critical details
- Don't print prompts — "Enter name:" will fail every test case
- Don't use nextInt() followed by nextLine() — Scanner trap will break your program
- Don't submit without removing debug prints — check every System.out line
- Don't assume case sensitivity — if the problem doesn't say, use equalsIgnoreCase to be safe
- Don't ignore the "not found" scenario — hidden test cases WILL check this
- Don't spend all time on one problem — if stuck after 30 min, move to the Unix problem
- Don't panic if hidden test cases fail — check output format, edge cases, and case sensitivity first
- Don't hardcode sample input values — your code must work for ANY input
- Don't type output strings from memory — copy-paste from the problem
Pre-Submit Checklist
Run through this checklist before hitting Submit. Every single item has caused students to fail:
| # | Check | Why |
|---|---|---|
| 1 | All debug prints removed? | Extra output = all test cases fail |
| 2 | No prompt messages? ("Enter:", "Input:") | Extra output = all test cases fail |
| 3 | Using nextLine().trim() for input? | Avoids Scanner trap and whitespace issues |
| 4 | Strings compared with .equals() or .equalsIgnoreCase()? | == compares addresses, not content |
| 5 | "Not found" case handled? | Hidden test case will check this |
| 6 | Output format matches problem exactly? | Character-by-character comparison |
| 7 | Decimal places correct? (%.2f if needed) | 95.5 vs 95.50 is a different string |
| 8 | Using long for sums? (not int) | Integer overflow on large inputs |
| 9 | All sample test cases passing? | If samples fail, hidden cases definitely fail |
| 10 | No hardcoded values? | Must read all values from input |