Skip to content

Start here: LeetCode from zero

New to coding? This page is for you. You do not need to know any algorithms yet. Read it once (about 15 minutes), then do your first problem.

Do this today (about 1 hour)
  1. Make a free LeetCode account and pick Go as the language.
  2. Read Go for Interviews once.
  3. Solve Contains Duplicate using the steps in section 4.
  4. Write two sentences in a notebook: what was the trick?

Can I start?

You only need the basics of any one language: variables, if statements, loops and functions. If those words are new, spend a week on a free beginner course first (for example the Tour of Go), then come back. That is completely fine.

Words you will see
Array / slice:
A numbered list of values, like [4, 7, 9]. In Go it is called a slice.
Hash map:
A lookup table. You give it a key (a name) and it gives back a value instantly.
Hash set:
Like a hash map, but it only remembers which keys it has seen.
Big-O:
A way to say how slow code gets when the input grows. O(n) means “one step per item”.
O(n²):
Two loops inside each other. Fine for 100 items, too slow for 100,000.
Recursion:
A function that calls itself on a smaller version of the same problem.
Brute force:
The simple, slow idea that tries everything. A good first step.
Edge case:
A strange input, like an empty list or one item, that can break your code.

The big idea

LeetCode is a website with coding puzzles. Companies use the same kind of puzzle in job interviews. Most people practise in a way that feels busy but does not work: they read answers and move on. Studies of how people learn, and advice from many experienced coders, agree on three simple rules:

  1. Learn patterns, not answers. A pattern is a trick that fits many problems. A small set of them covers most problems.
  2. Try first, then get small hints. Struggling for a short time is what makes the idea stick. Struggling for hours does not.
  3. Redo problems later. You remember what you recall again after a gap, not what you only re-read.
Loading diagram…
The loop for every problem. Steps 4 and 5 are the ones most people skip.

1. What a problem is

Every problem gives you a function signature, a few examples and some constraints. Your job is to write the function body so that it returns the right output for every valid input and runs fast enough. Take Contains Duplicate: given a list of numbers, return true if any number appears twice.

func containsDuplicate(nums []int) bool {
	// your code here
}

2. Set up in 5 minutes

  1. Make a free LeetCode account. In the editor, choose Go as the language. The editor runs your code against the tests.
  2. Optional but recommended: install Go so you can run code offline, or use the Go Playground in the browser.
  3. Read Go for Interviews once. It is short and covers slices, maps and the gotchas that waste beginners' time.

3. Read the constraints first

The size limit tells you which speed you are aiming for before you think of any idea. If a list can hold 100,000 numbers, an approach that compares every pair (about 10 billion steps) will time out. Look at the largest n in the constraints:

If n is…Aim forTypical tools
n ≤ 10O(n!)Try every ordering: permutations, backtracking.
n ≤ 20O(2ⁿ)Try every subset: backtracking, bitmasks.
n ≤ 500O(n³)Three nested loops are fine; interval DP lives here.
n ≤ 5,000O(n²)A DP table or two nested loops.
n ≤ 100,000O(n log n)Sort first, use a heap, or binary search.
n ≥ 1,000,000O(n) or O(log n)One pass: hash map, two pointers, sliding window, math.

New to this notation? The Big-O cheat sheet explains it with pictures.

4. Your first problem, step by step

Use the same five moves on every problem. Here they are on Contains Duplicate:

  1. Say it in your own words. “Is any number in the list repeated?”
  2. Work a tiny example by hand. [1, 2, 3, 1] → yes, 1 appears twice. [1, 2, 3] → no.
  3. Write the slow idea first. Compare every pair: two nested loops, O(n²). It is correct, and saying it out loud is a legitimate first step in an interview.
  4. Ask what is being repeated. The inner loop keeps asking “have I seen this number before?”. A hash set answers that in O(1), so keep a set of numbers seen so far.
  5. Code it, then test it. Try an empty list, a single number and an all-equal list before you submit.
func containsDuplicate(nums []int) bool {
	seen := map[int]bool{}
	for _, n := range nums {
		if seen[n] {
			return true // met this number before
		}
		seen[n] = true
	}
	return false
}

Step 4 is the whole skill. The rest of this site trains you to ask that question for every pattern.

Now you try it

Clear the code above from your head and write it yourself here, then press Run tests. It checks your answer against hidden tests, including a big one that is too slow for the nested-loop idea.

Write Go. Common packages like fmt and sort are imported for you. Keep the function name and inputs the same.

5. When you are stuck

Being stuck is normal. It is part of the lesson. Use this ladder, and only go down one step at a time:

Loading diagram…
Each problem on this site has a Nudge, an Idea and a Reference solution, in this order.
  • Nudge is a small push, like “what are you repeating?”
  • Idea tells you the approach in words, with no code.
  • Reference solution is the full code. Read it once, close it, then write it yourself.

6. What one good session looks like

Try alone20 minHint5–10Code + test10–15Write notes5
A normal 45–60 minute session for one Easy or Medium problem.
  1. Pick one problem from the roadmap. Just one.
  2. Set a 20-minute timer and try alone on paper.
  3. Use a hint if the timer ends. Then code it and test it with small examples.
  4. Write two sentences in a notebook: what the trick was, and how you could spot it next time.

Those two sentences are worth more than the code. They are how you build your own pattern list.

7. Redo problems (the secret step)

Your brain forgets new things fast. But each time you pull an idea out of memory without looking, it fades more slowly. This is called spaced practice plus self-testing. In a famous review of ten study methods, these two came out on top, while re-reading and highlighting came out low.

100%0%time since you first solved it →Solve once, never redo: gone fastDay 1Day 4Day 10Redo it: the memory lasts longer each time
Idea picture, not real data. The shape is what the research on spaced practice describes.
WhenWhat to do
Day 0Solve it. Close the solution and type it again from memory.
Day 1Solve it again on a blank page. No notes.
Day 3–4Solve it again. Say the idea out loud first.
Day 7–10Again. If you stall, go back one step (the Idea hint) and shorten the next gap.
Day 30Last check. If it is easy, you own it. Retire the problem.

The exact days are a simple plan based on several coders' advice; nobody has one proven schedule. What matters is that the gaps get longer. Use the Review page to practise recall with flashcards.

8. Learn the patterns

A pattern is a “if you see this, try that” rule. Here are the first ones to learn. You will meet each one in the roadmap:

If the problem says…TryExample
“Have I seen this before?”Hash set or mapContains Duplicate, Two Sum
Sorted list, find a pairTwo pointersTwo Sum II, 3Sum
Best / longest / shortest sub-part of a listSliding windowLongest Substring Without Repeating Characters
Sorted data, or “find the smallest value that works”Binary searchBinary Search, Koko Eating Bananas
Matching brackets, “undo” the last thingStackValid Parentheses
Every choice, try them allBacktrackingSubsets, Permutations
Tree or grid, go deep or go wideDFS / BFSNumber of Islands
Same small question again and againDynamic programmingClimbing Stairs, Coin Change

9. How many problems, and how much time?

  • Time: 45–60 minutes a day, 5 days a week, beats a 6-hour weekend. Short and regular wins because of the spacing effect above.
  • Number: The Tech Interview Handbook plan is 75 problems over 3 months (about 11 hours a week). This site's roadmap has 150 problems. You do not need all of them to start applying.
  • Quality: One problem you can explain and redo is worth more than five you only read.

10. Common mistakes

MistakeDo this instead
Staring at a blank editor for an hourSet a 20-minute timer. Then use a hint. Struggling helps; being lost does not.
Reading the answer and moving onClose it and type it yourself. Then redo it in a day.
Counting problems instead of learning50 problems done well beat 500 skimmed. Count patterns you can explain.
Memorising codeMemorised code breaks when the problem changes a little. Learn the idea behind it.
Never coming backOne solve is a guess. A solve three days later is proof.
Jumping to Hard problemsStay on Easy and Medium until the patterns feel boring.

11. A realistic first weeks

Loading diagram…
The order of the roadmap. Each step builds on the one before.
  1. Days 1–3
    Go basics, Big-O and recursion

    Read Go for Interviews, the Big-O cheat sheet and Recursion Basics. Run one tiny Go program. Do not solve problems yet.

  2. Days 4–7
    Arrays & Hashing

    Solve the 9 problems. Use the Nudge and Idea hints freely: the goal this week is the habit, not the score.

  3. Week 2
    Two Pointers, Stack, Binary Search

    About 2 problems a day. Re-solve anything you needed the Idea for.

  4. Weeks 3–4
    Sliding Window, Linked List, Trees

    Trees are the first topic where recursion is unavoidable, so do Recursion Basics first if you skipped it. Draw each example on paper.

  5. Weeks 5–10
    The rest of the map

    Heap, Backtracking, Graphs, DP and the others, in map order. Keep a short list of problems to redo every Sunday.