- Authors

- Name
- Stephen ♔ Ó Conchubhair
- Bluesky
- @stethewhitefox.bsky.social
Introduction
LeetCode is an online platform for coding interview preparation. It offers a wide range of coding problems that help developers improve their problem-solving skills and prepare for technical interviews. NeetCode is a YouTube channel that provides explanations and solutions to LeetCode problems, making it easier for learners to understand and apply coding patterns effectively.
So Why Practice Solving LeetCode/NeetCode Problems?
By choosing to learn how to solve LeetCode problems, I aim to master coding patterns and interviews, which heavily focus on data structures and algorithms (DSA) particularly used by the Big Tech companies (FAANG/MAANG).
My goal is not only to prepare for these challenging interviews but also to elevate my craft by honing my problem-solving skills and enhancing my approach to technical challenges, as it enables efficient problem-solving.
My Step-by-Step Approach
- Set a Timer:
- For the first try, start with: 40 minutes (Easy), 60 minutes (Medium), 100 minutes (Hard).
- For the second attempt, reduce to 20 minutes (Easy), 40 minutes (Medium), 60 minutes (Hard).
- Target times: 5 - 10 minutes (Easy), 10 - 20 minutes (Medium), 20 - 40 minutes (Hard).
- Understand the Problem:
- Read the problem statement carefully.
- Explain my understanding out loud in plain English to clarify the problem.
- Write and or edit my understanding of the problem in these blog posts.
- Think About Approach:
- Draw (if already drawn review and or edit) a Flow Chart or sketch:
- Helps in structuring my thoughts and planning my solution.
- Breaks the problem into smaller parts.
- Add comments to outline my thought process.
- Search for solutions if stuck:
- If bogged down, refer to NeetCode/LeetCode hints/solutions with YouTube and tools like copilot for insights.
- Try a Brute Force Solution:
- Implement a straightforward solution first in pseudocode.
- It may not be the most efficient but helps in understanding the problem better.
- Write Out the Solution Multiple Times:
- Practice coding the solution to improve muscle memory and familiarity.
- Evaluate and Improve: Checking if the solution can be optimized for efficiency (time and space complexity).
- Understand the code that solves the problem.
- Use TypeScript (TS):
- Ensure all solutions are written in TS to maintain consistency and leverage its type safety features. This is particularly useful for my full stack Web App development.
Extra Tips:
- Staying Consistent: Regular practice is key to improvement. Tackling at least one problem every day until I can solve it confidently without help, aiming for target times. Setting target times for solving coding problems is a great way to build efficiency and improve my problem-solving skills under time constraints.
- Review After Solving: Reflect on what went well and where I can improve by writing / editing these blog posts.
- Learn from Mistakes: Analyze where I got stuck and how I resolved it bug fix.
- Utilize Tools: For flow charts I am using miro.new and I am subscribed to YouTube channel NeetCode.
- A Little Break: Remember, if you feel overwhelmed at any point, taking a break is always a good idea. Sometimes stepping away for a little while can provide a fresh perspective when you return. I hope you enjoy my journey of mastering coding patterns!
Longest Common Prefix Problem
In this example, I'll apply my approach to solving NeetCode/Leetcode problems to solve this problem. The first pattern I'll use is the Vertical Scanning Pattern. I chose "Longest Common Prefix" as a common interview question. Mastering this pattern can boost your confidence and performance in technical interviews.
Using the LeetCode problem 14. Longest Common Prefix the task is to find the longest common prefix shared among an array of strings.
For example, given: ['flower', 'flow', 'flood']
the longest common prefix is: flo
Breakdown of the problem:
- Input: An array of strings.
- Output: The longest common prefix string among the input strings. If there is no common prefix, return an empty string "".
- Constraints: The input array can contain any number of strings, and the strings can be of varying lengths.
- Edge Cases: If the input array is empty, return an empty string. If there is no common prefix, return an empty string.
- Examples:
- Input:
["flower","flow","flight"]Output:"fl" - Input:
["dog","racecar","car"]Output:""
- Input:
- Iterate through the strings and compare the prefix with each string skipping the first one as it is already assigned to prefix
- While the current word does not begin with my current prefix, keep shrinking the prefix, removing the last character until it does or until the prefix is empty.
- return the longest common prefix found.
Pseudocode (Brute Force 💪)
// Returns the longest common prefix shared by all strings
function longestCommonPrefix(strs) :
if strs empty return ''
// Start with the first string as the initial prefix candidate
prefix = strs[0]
for i from 1 to strs.length - 1
while strs[i].indexOf(prefix) !== 0
// Shrink the prefix until it matches the start of the current string
prefix = prefix.substring(0, prefix.length - 1)
// No common prefix exists
if prefix.length == 0 return ''
return prefix
TypeScript (Efficient 🎯)
Intuition
The Vertical Scanning pattern compares characters at the same index across all strings to find the longest common prefix.
This is more efficient than the shrinking-prefix brute force approach because it allows us to stop as soon as we find a mismatch, instead of checking all possible prefixes.
Step-by-Step Approach:
- Check for edge case: If the input array is empty, return an empty string.
- Use the first string as a reference: iterate through each character of the first string.
- Select the first character: Use the char from the first string as a reference character for comparison.
- Perform Vertical comparison Compare that character with the characters at the same index in all other strings.
- Handle mismatch: If: - we reach the end of one string or - characters do not match return the substring from 0 to the current index.
- Return result: If the loop completes without finding a mismatch, the entire first string is a common prefix.
Complexity
Time: 𝑂(𝑛 ∗ 𝑚) 𝑛 = the number of strings 𝑚 is the length of shortest string in the array
Worst case: every character of every string matches, leading to 𝑂(𝑛 ∗ 𝑚) time complexity.
Space: 𝑂(1)
const longestCommonPrefix = (strs: string[]): string => {
if (strs.length === 0) return ''
for (let i = 0; i < strs[0].length; i++) {
// Get the current character from the first string
const char = strs[0][i]
// Compare with the same character in all other strings
for (let j = 1; j < strs.length; j++) {
// Check if we've reached the end of the current string or if the characters don't match
if (i >= strs[j].length || strs[j][i] !== char) {
// Return the common prefix up to this point
return strs[0].substring(0, i)
}
}
}
return strs[0]
}
const strs = ['Flowerpot', 'Flood', 'Flowing']
console.log(longestCommonPrefix(strs)) // Output: "Flo"
My Adventure in LeetCode/NeetCode
Join me on an uncomfortable yet educational, and at times a fun adventure into learning to problem-solve by solving NeetCode and LeetCode problems. As you read this blog post, feel free at any stage to dive into any of the patterns and explore the example problems associated with them. For me working on LeetCode problems sharpens both my theoretical knowledge and practical coding skills — making me a more robust and versatile developer.
Explore Patterns and Examples:
Photo by Gabriella Clare Marino on Unsplash
