Lesson 0.0 · Java for DSA
Start Here: Programming From Zero
Never written code before? Start here. What a program is, how to run Java on your computer, variables, decisions, loops and methods, what "data structures and algorithms" means, and how to move through this course one step at a time.
30 min
Think of it like this
A program is a recipe for a very obedient cook. The cook (the computer) does exactly what each line says, in order, and never guesses. If the recipe says "add 2 spoons of sugar" it adds 2, even if 3 would taste better. Learning to program is learning to write recipes that clear.
Data structures are how you arrange your ingredients (a spice rack sorted A–Z versus a heap on the table), and algorithms are the cooking steps. A good arrangement makes the steps quicker: you find the cinnamon in a second instead of searching the whole pile.
1.Your first program
Set up once. Install Java 21 (the free "Temurin JDK 21" from adoptium.net works on Windows, macOS and Linux). Open a terminal (Command Prompt on Windows) and type java -version: it should print a version starting with 21. Any code editor works; VS Code with the Java extension is a good free choice.
Write and run. Save the code below in a file named Main.java, then run java Main.java in the same folder. Java reads the file, checks it, and runs it. Every runnable example in this course has this shape, and you'll see its exact output underneath.
Reading it line by line. public class Main { … } is the box that holds your code. main is where the computer starts. System.out.println(...) prints one line. int apples = 3; makes a variable: a labelled box called apples holding the whole number 3. / divides whole numbers and drops the remainder; % gives the remainder. Every statement ends with ;.
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Hectal!");
int apples = 3; // a variable: a labelled box holding a value
int friends = 2;
System.out.println("Apples each: " + apples / friends); // whole-number division
System.out.println("Left over: " + apples % friends); // remainder
}
}Output
Hello, Hectal!
Apples each: 1
Left over: 12.Decisions and repetition
if lets the program choose: if (mark >= 40) { … } runs the block only when the condition is true. for repeats a block a counted number of times: for (int i = 0; i < 4; i++) runs with i = 0, 1, 2, 3. while repeats as long as a condition stays true.
An array like int[] marks = {72, 45, 90, 38}; is a row of numbered boxes. The first box is marks[0] (counting starts at 0), and marks.length is how many boxes there are. Almost every problem in this course starts with an array or a string.
public class Main {
public static void main(String[] args) {
int[] marks = {72, 45, 90, 38};
int passed = 0;
for (int i = 0; i < marks.length; i++) { // repeat for every mark
if (marks[i] >= 40) { // decide
passed++; // same as passed = passed + 1
}
}
System.out.println(passed + " of " + marks.length + " passed");
int n = 1;
while (n < 100) n = n * 2; // keep doubling until n reaches 100 or more
System.out.println("First power of two >= 100: " + n);
}
}Output
3 of 4 passed
First power of two >= 100: 1283.Methods: give a set of steps a name
A method is a named recipe you can use again and again. It takes inputs (parameters), does its steps, and gives back an answer with return. In coding problems you'll almost always write one method, such as int biggest(int[] nums), and the platform calls it with test inputs.
Below, biggest walks through the array once, remembering the largest value seen so far. Press play on the dry run to watch it: a dry run means following the code by hand, one step at a time, writing down what every variable holds. It's the single most useful habit in this course.
public class Main {
static int biggest(int[] nums) { // a method: inputs in, answer out
int best = nums[0];
for (int x : nums) { // "for each number x in nums"
if (x > best) best = x;
}
return best;
}
public static void main(String[] args) {
System.out.println(biggest(new int[]{4, 9, 2}));
System.out.println(biggest(new int[]{-5, -1, -8}));
}
}Output
9
-1nums = [4, 9, 2]variables(vars)
Step 1/4Start: best is the first number, 4.
4.What "data structures and algorithms" means
A data structure is a way of arranging data so the questions you ask are quick to answer. A paper list of names is fine for five friends; a phone's contact list sorted by name, with a search box, is what you need for five thousand. Arrays, hash maps, stacks, queues, trees, heaps and graphs are the standard arrangements, and this course teaches each one.
An algorithm is a precise list of steps that solves a problem, like the biggest method above. Two algorithms can give the same answer at very different speeds, and Big-O (Module 1) is how we compare them.
Companies test DSA in coding rounds because it shows how you think: can you understand a problem, find a correct approach, make it fast enough, write it cleanly and test it? Those are the same skills real software work needs every day.
Quick check
You keep 10 000 product prices and often need "the cheapest product right now" while prices change. Is that a data structure question or an algorithm question?
5.How to move through this course, step by step
Follow the order. Modules are arranged so each one only uses ideas from earlier ones: Java and Big-O → arrays and strings → hashing → array techniques → recursion → linear structures → searching → trees → graphs → dynamic programming → greedy → advanced topics → drills, the Core 100 and interview practice. Each module page shows what it builds on and what it unlocks.
Inside a module: read the lessons in order (each starts with a real-life picture, then the idea, code you can run, and a dry run), then solve the problems from easy to hard.
For every problem: read "In plain words", work the examples by hand, and try for 15–30 minutes. Stuck? Open one hint at a time. Then compare your approach with the ones shown (brute force first, then faster ones), and run the code yourself. Mark it complete only when you can write it again from a blank file.
Revise: come back to problems you needed help with after 1, 3 and 7 days. The Core 100 (Module 43) and the mock interviews (Module 44) at the end check that everything has stuck.
Remember
- A program runs your instructions exactly, in order.
- Variables, if, loops and methods are all you need to start.
- Dry-run code by hand: write what every variable holds at each step.
- Data structures arrange data; algorithms are the steps that use it.
- Follow the modules in order; try before reading the solution.
Common mistakes
- Copying solutions without trying first: you remember almost nothing that way.
- Skipping the dry runs: they're how you learn to see what code really does.
- Forgetting that arrays start at index 0.
Words used in this lesson
- Program
- A list of instructions the computer follows exactly.
- Variable
- A named box that holds a value, like `int apples = 3`.
- Loop
- Code that repeats: `for` counts, `while` repeats while a condition is true.
- Method
- A named set of steps with inputs and a returned answer.
- Array
- A row of numbered boxes; the first is at index 0.
- Dry run
- Following code by hand, step by step, tracking every variable.
- Data structure
- A way of arranging data so certain questions are fast to answer.
- Algorithm
- A precise list of steps that solves a problem.