This is a typescript repository of my personal responses to several Leetcode questions.
Each question has their own subfolder, with the following content:
problem.md- The problem description as seen in leetcodeproblem.test.ts- The test driver for the problem implementationsolution.ts- The implementation of the problemfiles- Optional folder that may contain images or files needed to run the problem
Feel free to use this repository as a template to practice your own implementations 💻👾🖱️
- #11. Container With Most Water
- #79. Word Search
- #125. Valid Palindrome
- #151. Reverse Words in a String
- #238. Product of Array Except Self
- #283. Move Zeroes
- #334. Increasing Triplet Subsequence
- #392. Is Subsequence
- #443. String Compression
- #547. Number of Provinces
- #643. Maximum Average Subarray I
- #841. Keys and Rooms
- #933. Number of Recent Calls
- #1071. Greatest Common Divisor of Strings
- #1431. Kids With the Greatest Number of Candies
- #1466. Reorder Routes to Make All Paths Lead to the City Zero
- #1493. Longest Subarray of 1's After Deleting One Element
- #1657. Determine if Two Strings Are Close
- #1679. Max Number of K-Sum Pairs
- #1732. Find the Highest Altitude
- #1768. Merge Strings Alternately
- #2215. Find the Difference of Two Arrays
- #2352. Equal Row and Column Pairs
- #2390. Removing Stars From a String
Like my dad always used to say, there's many ways to skin a cat. I was never sure what that meant, but I can tell you that there are a great many ways to approach a coding problem.
I have a lot of experience in software, and I have my own passion project to focus on. So for the most part, I avoided spending more than 10-15 minutes per problem. This means some of the code will be sloppy, and a lot of solutions are not optimal.
That's ok. I am not John Carmac, just a lowly programmer.
Assume you are in a programming interview, but you are a mid level engineer, not an expert. You will be working in typescript only. For the problem below, perform the following steps, in order.
- Verbally explain the naive solution (e.g. to pick the Kth largest element, we could simply sort this array and pick the Kth element from the end) and why you wouldn't want to implement that.
- Explain an improved ideal solution as a better alternative
- Write down your proposed solution as lines of comments that will be eventually expanded into actual code. This should be a numbered list, where instead of writing code like
myArray.At(-1)you would just writetake the last element. For example:
1. iterate through the array
2. for each item, store the key in the map
3a. if the value is null, return
3b. if the value is 0, add to the map
- Write actual code by expanding the single line comments into actual code.
- Perform a dry-run and mention as many optimizations as possible.
The easiest way to run a problem is with Bun. All you need to do is run a test file directly, i.e.:
# Run a specific test
bun test 151_reverse-words/01-problem-151.test.ts
# Run all tests
bun test
# Run all tests and watch for changes
bun test --watchIf you don't have/want Bun, you're crazy you can use Node if you want, but you will either need to install tsc to compile the typescript and then run it directly, or install a package like ts-node to run it directly. If you feel like going down that path, check out the NodeJs typescript documentation page.
You will also have to change the test files to use a test package like jest instead of the built in bun helpers.