Skip to content

Latest commit

 

History

History
527 lines (396 loc) · 14.3 KB

File metadata and controls

527 lines (396 loc) · 14.3 KB

🚀 LeetCode Algorithms & Data Structures

Professional JavaScript Solutions for MERN Stack Developers

License: MIT JavaScript Node.js PRs Welcome GitHub stars GitHub forks

🌟 Star this repo🐛 Report Bug✨ Request Feature


Master Data Structures & Algorithms with Production-Ready JavaScript Solutions


📖 Table of Contents


✨ Features

Feature Description
🎯 Organized by Difficulty Easy, Medium, Hard - structured for progressive learning
📝 Comprehensive Documentation Each solution includes detailed explanations, time/space complexity
🧪 Automated Testing Built-in test cases with colorful, readable output
🚀 Production-Ready Code ES6+ syntax, best practices, clean code principles
📊 Progress Tracking Metadata system to track your problem-solving journey
🎨 MERN Stack Optimized JavaScript solutions perfect for full-stack developers
🔍 Interview Prep Ready Common patterns, tricks, and optimization techniques
📦 Reusable Utilities Test runners, loggers, and helper functions

🎯 Why This Repository?

Stand Out in Technical Interviews 🎤

  • 300+ LeetCode Problems solved with optimal solutions
  • Real-world JavaScript patterns used in MERN stack projects
  • Interview-focused approach with time/space complexity analysis

Learn by Doing 📚

  • Progressive difficulty levels
  • Pattern recognition strategies
  • Multiple approaches for complex problems
  • Best practices from senior engineers

Production-Grade Code 💼

  • Clean, maintainable, and scalable code
  • Comprehensive test coverage
  • Industry-standard naming conventions
  • Professional documentation

📁 Repository Structure

leetcode-practice/
│
├── 📂 easy/                          # Easy difficulty problems
│   ├── 001-two-sum/
│   │   ├── index.js                  # Solution implementation
│   │   ├── test.js                   # Test cases
│   │   └── README.md                 # Problem explanation & approach
│   ├── 020-valid-parentheses/
│   └── ...
│
├── 📂 medium/                        # Medium difficulty problems
│   ├── 003-longest-substring/
│   ├── 015-three-sum/
│   └── ...
│
├── 📂 hard/                          # Hard difficulty problems
│   ├── 004-median-sorted-arrays/
│   ├── 023-merge-k-sorted-lists/
│   └── ...
│
├── 📂 utils/                         # Utility functions
│   ├── testRunner.js                 # Automated test execution
│   ├── logger.js                     # Colorful console logging
│   └── helpers.js                    # Common helper functions
│
├── 📂 config/                        # Configuration files
│   └── problems.json                 # Problem metadata & tracking
│
├── 📂 scripts/                       # Automation scripts
│   ├── testAll.js                    # Run all tests
│   └── createProblem.js              # Scaffold new problem
│
├── 📂 docs/                          # Additional documentation
│   ├── PATTERNS.md                   # Common algorithm patterns
│   ├── COMPLEXITY.md                 # Big O notation guide
│   └── TIPS.md                       # Interview tips & tricks
│
├── package.json                      # Project dependencies
├── .gitignore                        # Git ignore rules
├── LICENSE                           # MIT License
└── README.md                         # You are here!

🚀 Getting Started

Prerequisites

  • Node.js v18+ installed (Download here)
  • Basic understanding of JavaScript ES6+
  • Familiarity with Data Structures & Algorithms (optional, you'll learn!)

Installation

# 1. Clone the repository
git clone https://github.com/yourusername/leetcode-practice.git

# 2. Navigate to the project directory
cd leetcode-practice

# 3. Install dependencies
npm install

# 4. Run all tests to verify setup
npm test

Quick Start

# Run a specific problem's tests
node easy/001-two-sum/test.js

# Run all tests for a difficulty level
npm run test:easy
npm run test:medium
npm run test:hard

# Run all tests in the repository
npm test

📚 Problem Categories

Data Structures 🗂️

  • Arrays & Strings
  • Linked Lists
  • Stacks & Queues
  • Trees & Graphs
  • Hash Tables
  • Heaps & Priority Queues

Algorithms 🧮

  • Two Pointers
  • Sliding Window
  • Binary Search
  • Depth-First Search (DFS)
  • Breadth-First Search (BFS)
  • Dynamic Programming
  • Backtracking
  • Greedy Algorithms
  • Divide & Conquer

Patterns 🎯

  • Fast & Slow Pointers
  • Merge Intervals
  • Cyclic Sort
  • In-place Reversal
  • Tree Traversals
  • Topological Sort
  • K-way Merge

🧪 Running Tests

Each problem comes with comprehensive test cases:

// Example: easy/001-two-sum/test.js
const runTests = require('../../utils/testRunner');
const twoSum = require('./index');

const testCases = [
  {
    input: [[2, 7, 11, 15], 9],
    expected: [0, 1],
    description: 'Basic case: [2,7,11,15], target 9'
  },
  {
    input: [[3, 2, 4], 6],
    expected: [1, 2],
    description: 'Non-sequential indices'
  },
  {
    input: [[3, 3], 6],
    expected: [0, 1],
    description: 'Duplicate numbers'
  }
];

runTests(twoSum, testCases);

Output Example:

✅ Test 1 PASSED: Basic case: [2,7,11,15], target 9
✅ Test 2 PASSED: Non-sequential indices
✅ Test 3 PASSED: Duplicate numbers

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
   All 3 tests passed! 🎉
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

📝 Solution Format

Each problem folder follows this structure:

index.js - The Solution

/**
 * LeetCode #1: Two Sum
 * Difficulty: Easy
 * 
 * @description
 * Given an array of integers nums and an integer target,
 * return indices of the two numbers that add up to target.
 * 
 * @approach
 * Use a hash map to store complements as we iterate through the array.
 * For each number, check if its complement exists in the map.
 * 
 * @timeComplexity O(n) - single pass through array
 * @spaceComplexity O(n) - hash map storage
 * 
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
function twoSum(nums, target) {
  const map = new Map();
  
  for (let i = 0; i < nums.length; i++) {
    const complement = target - nums[i];
    
    if (map.has(complement)) {
      return [map.get(complement), i];
    }
    
    map.set(nums[i], i);
  }
  
  return [];
}

module.exports = twoSum;

README.md - Problem Documentation

Each problem includes:

  • Problem Statement - Clear description with examples
  • Approach - Step-by-step solution strategy
  • Complexity Analysis - Time and space complexity
  • Edge Cases - Special scenarios to consider
  • Alternative Solutions - Different approaches (if applicable)
  • Key Takeaways - Lessons learned

test.js - Automated Tests

  • Multiple test cases covering different scenarios
  • Edge case validation
  • Clear, descriptive test names
  • Automated pass/fail reporting

🎨 Code Quality

Naming Conventions

// ✅ Good
function calculateMaxProfit(prices) { ... }
const totalSum = prices.reduce((acc, curr) => acc + curr, 0);

// ❌ Avoid
function calc(p) { ... }
const x = prices.reduce((a, c) => a + c, 0);

Folder Naming

✅ Correct: 001-two-sum/
✅ Correct: 015-three-sum/
✅ Correct: 234-palindrome-linked-list/

❌ Avoid: twoSum/
❌ Avoid: 1-two-sum/
❌ Avoid: problem_001/

Best Practices

  • ✅ Use const and let, avoid var
  • ✅ Write descriptive variable names
  • ✅ Add comments for complex logic
  • ✅ Include JSDoc annotations
  • ✅ Handle edge cases
  • ✅ Optimize for time AND space when possible
  • ✅ Write modular, reusable code

🤝 Contributing

We welcome contributions! Here's how you can help:

Adding New Solutions

  1. Fork the repository
  2. Create a new branch
    git checkout -b feature/add-problem-123
  3. Follow the folder structure
    difficulty/###-problem-name/
    ├── index.js
    ├── test.js
    └── README.md
    
  4. Write comprehensive tests
  5. Document your approach
  6. Commit your changes
    git commit -m "Add: LeetCode #123 - Problem Name"
  7. Push to your fork
    git push origin feature/add-problem-123
  8. Open a Pull Request

Improving Existing Solutions

  • Found a more optimal solution? Submit a PR!
  • See a typo or bug? Fix it and contribute!
  • Better explanation? We'd love to see it!

Contribution Guidelines

  • Follow existing code style and conventions
  • Include test cases for all solutions
  • Update config/problems.json with problem metadata
  • Write clear commit messages
  • Add comments for complex algorithms

📊 Progress Tracker

Track your problem-solving journey:

Difficulty Solved Total Percentage
🟢 Easy 0 150 0%
🟡 Medium 0 150 0%
🔴 Hard 0 50 0%
Total 0 350 0%

By Topic

  • Arrays & Strings (0/50)
  • Linked Lists (0/25)
  • Trees & Graphs (0/40)
  • Dynamic Programming (0/35)
  • Backtracking (0/20)
  • Binary Search (0/15)
  • Sliding Window (0/18)
  • Two Pointers (0/22)

Update these counts as you add more solutions!


🔗 Useful Resources

Learning Resources

JavaScript Resources

MERN Stack


📜 License

This project is licensed under the MIT License - see the LICENSE file for details.

MIT License

Copyright (c) 2025 Your Name

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files...

💬 Contact

Your Name - MERN Stack Developer

LinkedIn GitHub Twitter Portfolio Email


🌟 Show Your Support

If this repository helped you in your interview preparation or learning journey, please consider:

  • Starring this repository
  • 🍴 Forking it for your own use
  • 📢 Sharing it with other developers
  • 🐛 Reporting issues or bugs
  • Contributing new solutions

Made with ❤️ by developers, for developers

⬆ Back to Top


Happy Coding! Keep learning, keep growing! 🚀