Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

🧩 LeetCode Solutions in JavaScript

A curated collection of my LeetCode solutions — written in JavaScript, documented with the reasoning behind each one.

JavaScript Node.js Jest LeetCode License


📌 About this repository

This repo is my personal, growing archive of solved LeetCode problems, implemented in JavaScript.

It's not just a dump of accepted submissions. For each problem I document the intuition, the approach, and the time/space complexity — because the goal isn't only to make the tests pass, it's to understand why a solution works and when it's the right tool.

I treat each problem as a rep in fundamentals: data structures, algorithms, and the trade-offs between them. Where a first solution passes but isn't optimal, I note it honestly and add the better approach — because a green checkmark tells you the output is correct, not that you solved the problem the right way.

What you'll find here:

  • ✅ Clean, readable JavaScript implementations
  • 🧠 The thought process behind each solution, not just the code
  • ⏱️ Complexity analysis for every problem
  • 🧪 Unit tests (Jest) covering edge cases, not just the happy path
  • 🏷️ Problems tagged by topic and difficulty for easy navigation

🗂️ Repository structure

Solutions are grouped by difficulty, and each problem lives in its own folder named by its LeetCode number and title:

leetcode-js/
├── src/
│   ├── easy/
│   │   ├── 0001-two-sum/
│   │   │   ├── solution.js
│   │   │   ├── solution.test.js
│   │   │   └── NOTES.md
│   │   └── 0704-binary-search/
│   │       ├── solution.js
│   │       ├── solution.test.js
│   │       └── NOTES.md
│   ├── medium/
│   │   └── 0003-longest-substring/
│   └── hard/
├── package.json
└── README.md

Each solution.js exports its function so the matching solution.test.js can import and test it; the NOTES.md explains the reasoning.


📖 How each solution is documented

Every problem follows the same structure, so the reasoning is consistent and easy to review. Solutions are written as plain functions and exported with module.exports:

/**
 * 704. Binary Search — https://leetcode.com/problems/binary-search/
 * Difficulty: Easy
 * Topics: Binary Search, Array
 *
 * Intuition:
 *   The array is sorted, so checking the middle element tells us which
 *   half the target must be in — letting us discard half the search
 *   space on every step instead of scanning linearly.
 *
 * Approach:
 *   Keep two bounds, left and right. Compare the middle value to the
 *   target and move the bound that keeps the target in range.
 *
 * Time:  O(log n)
 * Space: O(1)
 *
 * @param {number[]} nums - sorted ascending array
 * @param {number} target
 * @return {number} index of target, or -1 if absent
 */
function search(nums, target) {
  let left = 0;
  let right = nums.length - 1;

  while (left <= right) {
    const mid = Math.floor(left + (right - left) / 2); // avoids overflow patterns
    if (nums[mid] === target) return mid;
    else if (nums[mid] < target) left = mid + 1;
    else right = mid - 1;
  }
  return -1;
}

module.exports = search;

📊 Solutions index

# Problem Difficulty Topics Solution
0001 Two Sum 🟢 Easy Array, Hash Table JS
0125 Valid Palindrome 🟢 Easy Two Pointers, String JS
0704 Binary Search 🟢 Easy Binary Search JS
0167 Two Sum II - Input Array Is Sorted 🟡 Medium Two Pointers, Binary Search JS
0003 Longest Substring Without Repeating Characters 🟡 Medium Sliding Window, Hash Table JS

This table grows with every problem I solve. Difficulty legend: 🟢 Easy · 🟡 Medium · 🔴 Hard.


▶️ Getting started

You'll need Node.js (18+ recommended). Clone the repo and install the dev dependencies:

git clone https://github.com/oazevedolucas/leetcode-js.git
cd leetcode-js
npm install

Run all tests:

npm test

Run the tests for a single problem:

npx jest src/easy/0704-binary-search

Quickly try a solution in the REPL:

node -e "console.log(require('./src/easy/0704-binary-search/solution')([1,2,3,4,5], 4))"

🧪 Testing

Solutions are tested with Jest. A minimal package.json wires it up:

{
  "name": "leetcode-js",
  "version": "1.0.0",
  "scripts": {
    "test": "jest"
  },
  "devDependencies": {
    "jest": "^29.7.0"
  }
}

Each test file imports the exported solution and checks the canonical examples plus edge cases (empty inputs, duplicates, boundaries):

const search = require('./solution');

describe('704. Binary Search', () => {
  test('finds the target in the middle', () => {
    expect(search([-1, 0, 3, 5, 9, 12], 9)).toBe(4);
  });

  test('returns -1 when the target is absent', () => {
    expect(search([-1, 0, 3, 5, 9, 12], 2)).toBe(-1);
  });

  test('handles a single-element array', () => {
    expect(search([5], 5)).toBe(0);
  });
});

🏷️ Topics covered

As the collection grows, this section maps the algorithmic patterns practiced:

Array · Hash Table · Two Pointers · Binary Search · String · Sliding Window · Stack · Linked List · Trees · Dynamic Programming · Graphs


📈 Progress

Difficulty Solved
🟢 Easy
🟡 Medium
🔴 Hard
Total

Updated as I go. The number matters less than understanding each one.


🎯 Why I do this

Strong fundamentals are what let you reason about performance, choose the right data structure under pressure, and write code that scales. This repo is where I keep that muscle sharp — one problem at a time.


🔗 Let's connect

LinkedIn GitHub

Lucas Azevedo Souza — Software Engineer · JavaScript · Node.js · Backend

⭐ If you find any solution helpful, consider starring the repo!

About

Leet Code JavaScript

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages