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.

Why students fail
The #1 reason is I/O handling mistakes — not logic. Scanner bugs, extra spaces in output, wrong case in strings, debug prints left in code. Fix your I/O and you pass. Ignore it and you'll get 0 on hidden test cases even with correct logic.

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 stdinSystem.in in Java
  • Output goes to stdoutSystem.out in 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
Key concept
HackerRank is NOT an IDE. There are no prompts, no "Enter a number:" messages. Your program reads raw input and prints raw output. Nothing else. Think of it as: input in → processing → output out. That's it.

HSE Exam Format (HackerRank Software Engineering Assessment)

HSE stands for HackerRank Software Engineering Assessment. It is one of the toughest exams in TCS ILP.

DetailInfo
PlatformHackerRank
Key TopicsREST APIs are known to be critical. Java coding is likely.
TypeComplete programs with proper I/O — not fill-in-the-blank
FormatExact format may vary by batch — prepare broadly
What we know
  • 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
Preparation strategy
Since the exact format isn't fixed, focus on being strong in: REST API concepts (see REST / JSON / XML page), Java I/O and OOP, and HackerRank platform mechanics (covered on this page). These will help regardless of the specific questions.

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:

The 5-minute rule
Spend the first 5 minutes ONLY reading. Do not write a single line of code. Read the problem twice. Then trace through the sample input/output by hand. THEN start coding.
  1. Read the full problem statement — top to bottom, don't skim
  2. Read the "Note" section — this is where they hide critical info like "case-insensitive comparison" or exact output format
  3. Study the Input Format section — understand what each line of input contains
  4. Study the Output Format section — copy the exact string format. Letter by letter.
  5. Trace through Sample Input/Output — manually work through the example. Make sure your understanding matches
  6. Check the Constraints section — input size tells you if brute force is OK or you need optimization
  7. 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 ≤ 1000Brute force O(n²) is fine. If N ≤ 10⁶, you need O(n log n)
The confusing question trap
If a question feels confusing, don't panic. Focus ONLY on the sample input/output. Trace through it step by step. The sample tells you exactly what the program should do — even if the English description is unclear. Let the examples guide you, not just the text.

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 inputsc.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]);
Why this pattern?
Using 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
Rule: never mix nextInt/nextDouble with nextLine
Either use 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:

MistakeExampleResult
Extra trailing space"Hello " vs "Hello"FAIL
Wrong case"no gadget found" vs "No Gadget Found"FAIL
Extra blank lineExtra println() at endFAIL
Debug print left in"Debug: x=5" before outputFAIL
Wrong decimal places"95.5" vs "95.50"FAIL
Missing newline at endUsing 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
Pro tip
Copy the exact output string from the problem statement and paste it into your code. Don't type it from memory. One wrong capital letter = fail.

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:

TypeVisible?Purpose
Sample test cases (2-3)Yes — you see input, expected output, and your outputVerify 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
Important
Passing all sample test cases does NOT mean you'll pass hidden ones. Sample cases are the easiest — they test basic flow. Hidden cases test the tricky stuff.

What Hidden Test Cases Check

You can't see these, but you can predict them. Hidden cases almost always test these scenarios:

ScenarioWhat to handle
No match foundPrint the "not found" message exactly as specified
All elements matchReturn/print all of them, not just the first
Single elementArray with 1 item — does your code handle it?
Empty input (0 elements)Check for n=0 before looping
Case sensitivity"Sales" vs "sales" vs "SALES" — use equalsIgnoreCase
Large input (performance)1000+ objects — brute force O(n²) may timeout
Boundary values0, negative numbers, max int, very long strings
Duplicate valuesMultiple objects with same name/value — handle all of them
Whitespace in inputTrailing spaces — use .trim()
Test yourself before submitting
Mentally run your code against these scenarios. Ask: "What does my code do if zero elements match? What if all match? What if there's only one element?" If you can't answer, fix it before submitting.

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:

PhaseWhat to do
First 5-10 minRead ALL problems first. Start with whichever feels easiest.
Per problemPlan → Code → Test with sample → Check edge cases → Remove debug prints
Last 5-10 minReview all output formats, recheck edge cases, remove debug prints

Step-by-step for a coding problem:

  1. Read the problem twice — don't touch the keyboard yet
  2. Identify the class structure — what attributes, what types
  3. Write the class first — private fields, constructor, getters
  4. Write the input reading code — use the safe nextLine().trim() pattern
  5. Write the filtering/processing logic
  6. Handle the "not found" case
  7. Copy the exact output format from the problem statement
  8. Run against sample test cases
  9. Mentally check edge cases — 0 matches, all matches, 1 element
  10. Remove all debug prints
  11. Submit
If you're stuck
Don't stare at the problem. Focus on what you CAN do:
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

Avoid these at all costs
  • 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:

#CheckWhy
1All debug prints removed?Extra output = all test cases fail
2No prompt messages? ("Enter:", "Input:")Extra output = all test cases fail
3Using nextLine().trim() for input?Avoids Scanner trap and whitespace issues
4Strings compared with .equals() or .equalsIgnoreCase()?== compares addresses, not content
5"Not found" case handled?Hidden test case will check this
6Output format matches problem exactly?Character-by-character comparison
7Decimal places correct? (%.2f if needed)95.5 vs 95.50 is a different string
8Using long for sums? (not int)Integer overflow on large inputs
9All sample test cases passing?If samples fail, hidden cases definitely fail
10No hardcoded values?Must read all values from input
Final thought
HSE is not about being a genius programmer. It's about being precise. Read carefully, handle I/O correctly, match the output format exactly, and cover edge cases. Do these four things and you pass.