lowkey
← All guides

Stuck on a LeetCode problem? A step-by-step routine to get unstuck

Updated November 30, 2026 · 7 min read

You've read the problem twice, you kind of get it, and you have no idea where to start. Or you have a start and it's going nowhere. Being stuck is normal, even for people who are good at this. What separates them is that they have a routine for getting unstuck instead of just waiting for an idea to show up.

Here's that routine. Go through the steps in order. Each one is small, and most of the time one of them breaks the problem open before you reach the end.

Step 1: Restate the problem in your own words

Write one or two sentences that describe what the function takes in and what it returns, without copying the problem text. This sounds too simple, but a surprising amount of stuck comes from a misread. Common ones:

  • Is it a subarray (contiguous) or a subsequence (any order kept, gaps allowed)?
  • Do they want the count, the length, the indices, or the actual elements?
  • Is the input sorted? Can it have duplicates? Negative numbers?
  • Can you modify the input, or do you need to keep it unchanged?

Step 2: Read the constraints like a hint

The constraints section is the problem setter quietly telling you the intended complexity. A rough guide:

  • n up to about 20: exponential is fine (backtracking, bitmasks).
  • n up to a few thousand: O(n^2) is probably fine.
  • n up to 10^5 or 10^6: you need O(n log n) or O(n). Think sorting, hashing, two pointers, sliding window, heaps, or binary search.
  • Values huge but n small, or the reverse: that difference is usually the point.

Step 3: Solve tiny examples by hand

Make up two or three small inputs, including one weird one (empty, one element, all the same). Solve them on paper without thinking about code. Then look at what you did. Did you scan left to right? Keep track of something as you went? Go back and fix an earlier choice? Your manual process is often the algorithm, just not written down yet.

Step 4: Write the brute force, even if it's slow

Say out loud, or write as a comment, the most obvious correct solution, no matter how slow. "Try every start and end index." "Try every subset." This does two things. It proves you understand the problem, and it gives you something concrete to improve. In an interview, stating the brute force first is expected anyway.

Step 5: Find the repeated work

Ask: what is the brute force doing over and over? Almost every optimization is removing repeated work:

  • Searching for the same thing many times: store it in a hash map or set.
  • Recomputing a sum over overlapping ranges: use prefix sums or a sliding window.
  • Solving the same subproblem many times: memoize it, which is dynamic programming.
  • Scanning for the next bigger or smaller element repeatedly: a monotonic stack.
  • Repeatedly finding the smallest or largest item: a heap.
  • Checking every candidate answer when the answers are ordered: binary search on the answer.

Step 6: Try the usual tools one at a time

If step 5 didn't point anywhere, go through a short list and ask "what would this look like with..." for each one: sorting first, a hash map, two pointers, a sliding window, a stack, BFS or DFS, a heap, binary search. You're not guessing randomly. You're checking each tool against what you learned in steps 2 to 5, and most will be ruled out in seconds.

A worked example

Take "Longest Substring Without Repeating Characters": given a string, return the length of the longest substring with no repeated characters.

Restate and check constraints

Substring means contiguous. We return a length, not the substring. The string can be up to 5 times 10^4 characters, so O(n^2) is borderline and O(n) is the target.

Tiny example by hand

For "abcabcbb", I start at a, extend to abc, then hit another a. By hand, I don't restart from b and rescan. I just drop the first a and keep going with bca. That's the key observation, and I found it by watching what I did.

Brute force and repeated work

Brute force: for every start index, extend until you see a repeat, which is O(n^2). The repeated work is that after a repeat, we throw away everything we know and rescan characters we already checked.

Pick the tool

"Contiguous range that grows and shrinks" plus "throwing away known work" says sliding window. We need to know quickly whether a character is in the window, so a set or a map of last-seen positions.

def length_of_longest_substring(s):
    last_seen = {}
    left = 0
    best = 0
    for right, ch in enumerate(s):
        if ch in last_seen and last_seen[ch] >= left:
            left = last_seen[ch] + 1
        last_seen[ch] = right
        best = max(best, right - left + 1)
    return best

Notice that nothing in this process required remembering the problem. Each step pointed at the next.

When the routine doesn't work

Sometimes you go through every step and still have nothing. That usually means the problem needs a pattern you haven't learned yet, and no amount of staring will produce it. That's the moment to take a small hint, like the topic tag or the first sentence of an approach, and go back to solving. The guide on practicing without the solution covers how to take hints without wasting the problem.

Two quick resets that help more than they should: explain the problem out loud to an imaginary friend, and take a five-minute walk. Both force you out of the loop of rereading the same line.

The checklist

  1. Restate the input and output in your own words.
  2. Use the constraints to guess the target complexity.
  3. Solve two or three tiny examples by hand and watch what you do.
  4. Write the brute force.
  5. Find the repeated work in the brute force.
  6. Try each common tool against what you've learned.
  7. Still stuck? Take the smallest hint you can, then keep going.