Red-Green-Code

Deliberate practice techniques for software developers

  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter

LeetCode Tip 28: More Advice for Writing Memorable Solutions

By Duncan Smith Jul 19 0

LeetCode 2023

A model solution shouldn’t just explain how to solve a problem. It should also help you remember the solution, so you can use it in the future for similar problems. Here are a few more ideas for writing solutions you’ll remember.

Use multiple levels of abstraction

Think of a model solution like a pyramid, with the submitted code at the base and a tag like “binary search” or “hash table” at the apex. At the base, all the details are worked out, and all ambiguity has been clarified. That’s what is required to write instructions that can be understood by a computer. At the tip, there are just a few words that name the concept that the problem covers. Between these two extremes, you can explain the problem at different levels. For example, to elaborate on the “binary search” tag, you could say, “Run a binary search on the sorted input array by checking the middle value, then checking either the left or right half depending on how the middle value compares to the target. Repeat until you either find the target or run out of input values to search.”

By writing a solution at multiple levels of abstraction, you can make the solution useful regardless of how much of it you remember. If you just need a small hint, the topic tag may be enough. If you can’t figure out how the topic applies to the problem, you can move lower in the pyramid to get a bit more detail. This is often how LeetCode hints are structured, so people can read just the first hint if that’s all they need, or multiple hints if they need more help.

Divide your solution into chunks

Another way to break up a model solution uses the concept of chunking from cognitive psychology. According to this theory, human working memory has a limited capacity, on the order of seven items. If the number of items you’re working with gets too large, some items are lost.

The reason we can solve complex problems despite this memory limitation is that each item can contain a collection of smaller items. For example, if you try to learn a solution by remembering individual lines, you’ll quickly run out of working memory when you try to reproduce it. If you instead chunk multiple lines into a block of code, that will considerably reduce the number of chunks in a solution. Once you learn a solution well, you could even retrieve the entire solution as one chunk.

As you write your model solution, think about how to divide it into chunks, to reduce the demands on your short-term memory.

Explain the solution in multiple ways

You may have had the experience of reading a solution that doesn’t clarify anything about the problem. Sometimes that’s because the solution is just poorly written. But it could also be that you’re just missing some essential context. When you write your own model solution, you can only use the background you currently have. A year later, you might come to it with a different background. Keep this in mind as you repeat problems and revise your model solutions. As you gain experience, you may be able to write a different solution to the same problem, even if you don’t change the algorithm. Both solutions may be well-written, and the earlier one may even be better for an audience with less experience. But the later one makes use of what you learned in the intervening months. When you gain enough experience, it may make sense to rewrite a solution from your new perspective.

This year, I’m publishing a series of tips for effective LeetCode practice. To read the tips in order, start with A Project for 2023.

Categories: LeetCode

Prev
Next

Stay in the Know

I'm trying out the latest learning techniques on software development concepts, and writing about what works best. Sound interesting? Subscribe to my free newsletter to keep up to date. Learn More
Unsubscribing is easy, and I'll keep your email address private.

Getting Started

Are you new here? Check out my review posts for a tour of the archives:

  • 2023 in Review: 50 LeetCode Tips
  • 2022 in Review: Content Bots
  • 2021 in Review: Thoughts on Solving Programming Puzzles
  • Lessons from the 2020 LeetCode Monthly Challenges
  • 2019 in Review
  • Competitive Programming Frequently Asked Questions: 2018 In Review
  • What I Learned Working On Time Tortoise in 2017
  • 2016 in Review
  • 2015 in Review
  • 2015 Summer Review

Archives

Recent Posts

  • Do Coding Bots Mean the End of Coding Interviews? December 31, 2024
  • Another Project for 2024 May 8, 2024
  • Dynamic Programming Wrap-Up May 1, 2024
  • LeetCode 91: Decode Ways April 24, 2024
  • LeetCode 70: Climbing Stairs April 17, 2024
  • LeetCode 221: Maximal Square April 10, 2024
  • Using Dynamic Programming for Maximum Product Subarray April 3, 2024
  • LeetCode 62: Unique Paths March 27, 2024
  • LeetCode 416: Partition Equal Subset Sum March 20, 2024
  • LeetCode 1143: Longest Common Subsequence March 13, 2024
Red-Green-Code
  • Home
  • About
  • Contact
  • Project 462
  • CP FAQ
  • Newsletter
Copyright © 2025 Duncan Smith