Step 01 Beginner Problems — Language Basics
Learn Java
Java is the language of enterprise software and a very common interview choice: everything lives inside classes, the Collections framework is excellent, and the compiler catches many mistakes early. This lesson is an orientation — the class skeleton every program shares, the two ways to read input, the core syntax for this sheet, and what to learn next.
The class skeleton
A Java program is a class with a main method; the file name must match the public class name. There is no way around this boilerplate, so memorise the frame once and reuse it forever. System.out.println prints a line; System.out.print stays on the same line.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long sum = 0;
for (int i = 0; i < n; i++) {
sum += sc.nextInt();
}
System.out.println(sum);
}
}Reading input: Scanner vs BufferedReader
Scanner is simple — nextInt(), nextLong(), next() for a word, nextLine() for a full line — and is fine for learning and small inputs. Its trap: after nextInt(), the newline stays in the buffer, so a following nextLine() returns an empty string; add a throwaway nextLine() call after numeric reads when you mix the two.
For large inputs Scanner is slow, and competitive code switches to BufferedReader with StringTokenizer, parsing numbers manually. Know that the faster route exists; adopt it when a problem actually times out, not before.
The core syntax tour
Types: int, long (append L to big literals), double, char, boolean, and String — capital S, because String is a class, which is also why you compare strings with .equals(), never ==. Loops and conditions mirror C++ closely, and methods are just functions inside the class — declare helpers static so main can call them directly.
Arrays are objects with a .length; the flexible list is ArrayList<Integer> — note the capitalised wrapper classes (Integer, Long) because collections cannot hold raw primitives. Strings are immutable: building one with += in a loop creates a new string each time, so use StringBuilder when assembling long output or reversed strings.
Your Java road map for this sheet
In order: (1) the skeleton, loops and methods until patterns feel mechanical; (2) arrays and strings, including StringBuilder; (3) recursion; (4) the Java Collections lesson in this module — ArrayList, HashMap, HashSet, PriorityQueue, Deque and the Collections utility methods. Object-oriented design (inheritance, interfaces) matters for development interviews but can wait until your DSA base is built.
Key points
- Java frame: public class matching the file name, static main, import java.util.* — memorise it once.
- Scanner is easy but slow and has the leftover-newline trap after nextInt(); BufferedReader is the fast route.
- Compare strings with .equals(), never ==; build long strings with StringBuilder, not += in a loop.
- Collections need wrapper types (Integer, not int) — ArrayList<Integer> is your default dynamic array.
- Road map: loops/methods → arrays/strings → recursion → Collections.