The Quest Begins (The “Why”)
I still remember staring at my screen after yet another failed attempt at a medium‑difficulty LeetCode problem. My fingers were tangled in a mess of nested loops, I kept mixing up indices, and the clock ticked past midnight. I felt like Neo in the first Matrix movie — trapped in a simulation where every move seemed to trigger a bug. I’d solved a few easy problems by brute force, but the moment the pattern got trickier, I hit a wall. The frustration wasn’t just about getting a wrong answer; it was the sinking feeling that I was memorizing solutions instead of truly understanding them. I knew I needed a different approach, something that would turn passive scrolling into active mastery.
The Revelation (The Insight)
After a weekend of trial and error, I stumbled on a simple, repeatable routine that changed everything:
“Explain, then repeat from memory after a delay.”
That’s it. The technique is literally two steps, but the power comes from how you execute them.
- Solve the problem (any language, any approach).
- Close your editor. Explain the solution out loud — or write it down — as if you’re teaching a complete beginner. Walk through every variable, every loop condition, every edge case.
- Wait. Give yourself a short break (10 minutes works, or sleep on it if you can).
- Re‑solve the problem from scratch, without looking at your notes or the original solution.
If you get stuck, peek only at the explanation you gave yourself, not the code. Then close it again and try once more.
Why does this work?
- Explaining forces you to articulate the *why* behind each line, exposing gaps in understanding that silent coding hides.
- Delaying the second attempt triggers active recall, which is proven to strengthen memory far more than re‑reading.
- By limiting yourself to the explanation as a hint, you avoid the trap of simply copying the solution verbatim.
I first tried it on the classic “Two Sum” problem. After my initial messy solution, I spent two nested loops, O(n²) ), I closed the laptop, stood up, and said:
“We need to find two numbers that add up to a target. As we iterate through the array, we can store each number’s complement (target − num) in a hash map. If we ever see a number that’s already in the map, we’ve found the pair.”
Hearing those words out loud made the hash‑map idea click. After a 10‑minute break, I opened a fresh file and wrote the O(n) solution in under two minutes — no peeking. The feeling was like finally dodging bullets in slow‑mo.
Wielding the Power (Code & Examples)
The Struggle (Before)
Here’s what my first attempt at “3Sum” looked like — a tangled mess of three loops and duplicate‑skipping logic that I kept getting wrong:
def threeSum(nums):
res = []
nums.sort()
for i in range(len(nums)):
for j in range(i+1, len(nums)):
for k in range(j+1, len(nums)):
if nums[i] + nums[j] + nums[k] == 0:
triple = [nums[i], nums[j], nums[k]]
if triple not in res: # awful O(n) check
res.append(triple)
return res
Enter fullscreen mode Exit fullscreen mode
What’s wrong?
- O(n³) time — far too slow for LeetCode’s constraints.
- The
if triple not in rescheck is both inefficient and error‑prone. - I was blindly looping without thinking about the sorted array’s properties.
The Revelation Applied (After)
I closed the editor, explained the solution:
“First sort the array. Then fix one number at index i. For the remaining part, use two pointers — left at i+1, right at the end. If the sum is too low, move left up; if too high, move right down. When we hit zero, record the triplet and skip duplicates by moving pointers past equal values.”
After a short break, I wrote the clean version:
def threeSum(nums):
nums.sort()
res = []
n = len(nums)
for i in range(n - 2):
# skip duplicate fixed numbers
if i > 0 and nums[i] == nums[i-1]:
continue
left, right = i + 1, n - 1
while left < right:
s = nums[i] + nums[left] + nums[right]
if s == 0:
res.append([nums[i], nums[left], nums[right]])
# skip duplicates for left and right
while left < right and nums[left] == nums[left+1]:
left += 1
while left < right and nums[right] == nums[right-1]:
right -= 1
left += 1
right -= 1
elif s < 0:
left += 1
else:
right -= 1
return res
Enter fullscreen mode Exit fullscreen mode
What changed?
- The explanation forced me to recognize the two‑pointer pattern.
- Skipping duplicates became explicit, removing the costly
not in rescheck. - The algorithm is now O(n²) — the optimal solution for this problem.
Common Traps to Avoid
Trap What it looks like Why it hurts How the technique prevents it Copy‑pasting Opening the solution tab, copying code, running it. You never internalize the logic; you’ll fail on a variation. You must close the editor and explain before you can even look at the code again. Skipping the explanation Solving, then immediately re‑solving from memory. You rely on muscle memory, not understanding; you miss why a step exists. The explanation step is mandatory — it’s the “Feynman” part that uncovers gaps. No delay Explain, then instantly re‑solve. The material is still in short‑term memory; you’re not testing true recall. A short break (or sleep) forces retrieval from long‑term memory, strengthening it.Why This New Power Matters
Adopting this “Explain, then repeat” loop turned my LeetCode practice from a grind into a game of discovery. I started noticing patterns everywhere — sliding window, fast/slow pointers, monotonic stack — because each time I explained a solution, I was essentially labeling the pattern for my brain.
Within a month, I went from struggling with medium problems to consistently solving hard ones in under 15 minutes. More importantly, I could adapt known patterns to new variations without panicking. The confidence spilled over into interviews: I could walk interviewers through my thought process, whiteboard a solution, and still have time to discuss trade‑offs.
If you’re just starting out, don’t waste hours rereading solutions. Pick one problem today — any easy or medium you’ve seen before — and apply the exact steps: solve, explain, wait, re‑solve from memory. Do it for three problems in a row, then take a day off and try them again cold. You’ll feel the difference almost instantly.
Your Turn
Grab a problem you’ve solved before (maybe “Valid Parentheses” or “Best Time to Buy and Sell Stock II”). Close your IDE, explain the solution out loud as if you’re teaching a friend, wait 10 minutes, then code it again from scratch. Come back and tell me how it felt — did the “aha!” moment arrive faster?
Now go forth, hack the Matrix, and remember: the real power isn’t in memorizing code — it’s in being able to recreate it whenever you need it. Happy coding! 🚀
답글 남기기