Skip to content

About

A collection of LeetCode problem solutions, rigorously tested with GoogleTest for C++ and pytest for Python.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

ย 

History

254 Commits

Folders and files

Repository files navigation

Unit-Tested LeetCode Solutions

๐Ÿ”ฌ Code Health & Testing

C++ Tests Python Tests

๐Ÿ” Code Quality & Linting

C++ Linter Python Linter

๐Ÿ“Š Repository Stats

Last Commit C++ Solutions Python Solutions

Description

This repository contains comprehensive, unit-tested solutions to LeetCode problems implemented in both C++20 and Python 3. Each solution includes:

  • ๐Ÿงช Comprehensive test suites with multiple test cases
  • ๐Ÿ“ Detailed documentation with complexity analysis
  • ๐Ÿ”ง Automated code quality checks and formatting
  • ๐Ÿš€ CI/CD pipeline with automated testing and linting

๐Ÿ“ Project Structure

โ”œโ”€โ”€ problems/                    # Problem solutions organized by name
โ”‚   โ”œโ”€โ”€ defs.bzl                 # Starlark macro for defining problem targets
โ”‚   โ”œโ”€โ”€ two_sum/                 # Individual problem directories
โ”‚   โ”‚   โ”œโ”€โ”€ BUILD.bazel          # Problem target definitions
โ”‚   โ”‚   โ”œโ”€โ”€ config.yml           # Problem metadata and configuration
โ”‚   โ”‚   โ”œโ”€โ”€ two_sum.py           # Python solution
โ”‚   โ”‚   โ”œโ”€โ”€ two_sum.cc           # C++ solution
โ”‚   โ”‚   โ”œโ”€โ”€ two_sum.h            # C++ header
โ”‚   โ”‚   โ”œโ”€โ”€ two_sum_test.py      # Python unit tests
โ”‚   โ”‚   โ””โ”€โ”€ two_sum_test.cc      # C++ unit tests
โ”‚   โ””โ”€โ”€ ...
โ”œโ”€โ”€ common/                      # Shared data structures (e.g. TreeNode)
โ”œโ”€โ”€ config/                      # Global configuration files
โ”‚   โ”œโ”€โ”€ difficulties.yml         # Difficulty level definitions
โ”‚   โ””โ”€โ”€ tags.yml                 # Problem tag categories
โ”œโ”€โ”€ scripts/                     # Automation and utility scripts
โ”‚   โ”œโ”€โ”€ format.sh                # Code formatting (C++ & Python)
โ”‚   โ”œโ”€โ”€ lint.sh                  # Code linting (C++ & Python)
โ”‚   โ”œโ”€โ”€ install_hooks.sh         # Git hooks installer
โ”‚   โ”œโ”€โ”€ uninstall_hooks.sh       # Git hooks uninstaller
โ”‚   โ”œโ”€โ”€ generate_readme.py       # Auto-generate README content
โ”‚   โ”œโ”€โ”€ update_badges.py         # Update repository badges
โ”‚   โ””โ”€โ”€ update_badges.sh         # Badge update automation
โ”œโ”€โ”€ tools/                       # Build tools & test runners
โ”œโ”€โ”€ .github/workflows/           # CI/CD automation
โ”‚   โ”œโ”€โ”€ cpp-test.yml             # C++ Bazel testing
โ”‚   โ”œโ”€โ”€ python-test.yml          # Python Bazel testing
โ”‚   โ”œโ”€โ”€ cpp-lint.yml             # C++ clang-tidy and clang-format
โ”‚   โ”œโ”€โ”€ python-lint.yml          # Python ruff lint and format
โ”‚   โ””โ”€โ”€ pr-size-labeler.yml      # PR size labeling
โ”œโ”€โ”€ MODULE.bazel                 # External dependency management (Bzlmod)
โ”œโ”€โ”€ BUILD.bazel                  # Root package definition
โ”œโ”€โ”€ .bazelrc                     # Bazel compiler flags & options
โ”œโ”€โ”€ requirements.txt             # Python dependencies
โ”œโ”€โ”€ requirements_lock.txt        # Pinned dependencies for rules_python
โ”œโ”€โ”€ .clang-format                # C++ code formatting rules
โ””โ”€โ”€ .clang-tidy                  # C++ linting configuration

๐Ÿ› ๏ธ Technologies & Tools

Languages & Standards

  • C++: C++20 with modern features and strict compiler warnings (-std=c++20 -Wall -Wextra -Wpedantic)
  • Python: Python 3.14 with type hints and modern syntax

Testing Frameworks

  • C++: Google Test (googletest@1.18.0.bcr.1) hermetically managed via Bazel Bzlmod
  • Python: pytest hermetically managed via rules_python

Code Quality Tools

  • C++ Formatting: clang-format for consistent code style
  • C++ Linting: clang-tidy for static analysis and best practices
  • Python Formatting & Linting: ruff for fast formatting and linting

Build & Automation

  • Bazel: Multi-language, hermetic build and test system with sandboxed execution and transitive caching
  • GitHub Actions: Automated CI/CD running native Bazel test targets

๐Ÿงช Running Tests with Bazel

All test targets run sandboxed and in parallel with action caching. No system installation of Google Test or CMake is required.

# Run all tests (both C++ and Python) across the entire repo
bazel test //problems/...

# Run all tests for a specific problem (both languages)
bazel test //problems/two_sum:test

# Run only C++ tests
bazel test //problems/two_sum:cc_test          # for a specific problem
bazel test --test_tag_filters=cc //problems/... # for all problems

# Run only Python tests
bazel test //problems/two_sum:py_test          # for a specific problem
bazel test --test_tag_filters=py //problems/... # for all problems

๐ŸŽจ Code Quality

This project maintains high code quality standards through automated tooling:

Formatting & Linting

# Format code (C++ with clang-format, Python with ruff)
./scripts/format.sh          # format both
./scripts/format.sh cpp      # format C++ only
./scripts/format.sh py       # format Python only

# Lint code (C++ with clang-tidy, Python with ruff)
./scripts/lint.sh            # lint both
./scripts/lint.sh cpp        # lint C++ only
./scripts/lint.sh py         # lint Python only

Git Pre-Commit Hooks

Install the pre-commit hook to automatically format, lint, update badges, and run tests before committing:

./scripts/install_hooks.sh

Automation & Documentation

# Regenerate README from problem configurations
bazel run //scripts:generate_readme

# Update README badges with current solution counts
bazel run //scripts:update_badges
# or: ./scripts/update_badges.sh

# Clean build artifacts
bazel clean

๐Ÿ”„ Continuous Integration

The GitHub Actions CI pipeline runs 4 focused workflows on both pull requests and pushes to main:

  • โšก C++ / Test (cpp-test.yml): Parallel sandboxed GoogleTest execution via Bazel
  • ๐Ÿ Python / Test (python-test.yml): Parallel sandboxed pytest execution via Bazel
  • ๐Ÿ” C++ / Lint (cpp-lint.yml): Static analysis (clang-tidy) and format enforcement (clang-format)
  • ๐Ÿ” Python / Lint (python-lint.yml): Ruff linting and format enforcement

๐Ÿงฎ Algorithms & Data Structures

This repository covers a comprehensive range of algorithmic patterns and data structures commonly found in technical interviews:

Array / String

# Title Difficulty Approach Solution Time Space Note
6 Zigzag Conversion Medium Row Buckets Python, C++ O(n) O(n)
Math Python, C++ O(n) O(1)
12 Integer to Roman Medium Math Python, C++ O(1) O(1)
13 Roman to Integer Easy Math Python, C++ O(n) O(1)
14 Longest Common Prefix Easy Vertical Scanning Python, C++ O(n * m) O(1)
Horizontal Scanning Python, C++ O(n * m) O(1)
Min / Max Lexicographical Python, C++ O(n * m) O(1)
Trie Python, C++ O(n * m) O(n * m)
26 Remove Duplicates from Sorted Array Easy Two Pointers Python, C++ O(n) O(1)
27 Remove Element Easy Two Pointers Python, C++ O(n) O(1)
28 Find the Index of the First Occurrence in a String Easy Brute Force Python, C++ O(n * m) O(1)
Knuth-Morris-Pratt (KMP) Algorithm Python, C++ O(n + m) O(m)
42 Trapping Rain Water Hard Brute Force Python, C++ O(n^2) O(1)
Dynamic Programming Python, C++ O(n) O(n)
Monotonic Stack Python, C++ O(n) O(n)
Two Pointers Python, C++ O(n) O(1)
45 Jump Game II Medium Dynamic Programming Python, C++ O(n^2) O(n)
Greedy Python, C++ O(n) O(1)
55 Jump Game Medium Dynamic Programming Python, C++ O(n^2) O(n)
Greedy Python, C++ O(n) O(1)
58 Length of Last Word Easy String Traversal Python, C++ O(n) O(1)
68 Text Justification Hard Greedy Python, C++ O(k * maxWidth), where k is the number of lines O(maxWidth)
80 Remove Duplicates from Sorted Array II Medium Two Pointers Python, C++ O(n) O(1)
88 Merge Sorted Array Easy Two Pointers Python, C++ O(m + n) O(1)
121 Best Time to Buy and Sell Stock Easy Dynamic Programming Python, C++ O(n) O(1)
Greedy Python, C++ O(n) O(1)
122 Best Time to Buy and Sell Stock II Medium Dynamic Programming Python, C++ O(n) O(1)
Greedy Python, C++ O(n) O(1)
134 Gas Station Medium Greedy Python, C++ O(n) O(1)
135 Candy Hard Greedy (Two Passes) Python, C++ O(n) O(n)
151 Reverse Words in a String Medium In-Place Reversal Python, C++ O(n) O(1)
169 Majority Element Easy Boyer-Moore Majority Vote Algorithm Python, C++ O(n) O(1)
189 Rotate Array Medium Repeated Shift Python, C++ O(nk) O(1)
Extra Space Python, C++ O(n) O(n)
Reversal Python, C++ O(n) O(1)
238 Product of Array Except Self Medium Prefix Sum Python, C++ O(n) O(1)
274 H-Index Medium Sorting Python, C++ O(n log n) O(1)
Counting Sort Python, C++ O(n) O(n)
380 Insert Delete GetRandom O(1) Medium Array & Hashmap Python, C++ O(1) O(n)

Two Pointers

# Title Difficulty Approach Solution Time Space Note
11 Container With Most Water Medium Two Pointers Python, C++ O(n) O(1)
15 3Sum Medium Sorting & Two Pointers Python, C++ O(n^2) O(1) You have to avoid duplicates without using sets.
125 Valid Palindrome Easy Two Pointers Python, C++ O(n) O(1)
167 Two Sum II - Input Array Is Sorted Medium Two Pointers Python, C++ O(n) O(1)
392 Is Subsequence Easy Two Pointers Python, C++ O(n) O(1)

Hashmap

# Title Difficulty Approach Solution Time Space Note
1 Two Sum Easy Brute Force Python, C++ O(n^2) O(1)
Hash Map Python, C++ O(n) O(n)
49 Group Anagrams Medium โ€” Python, C++ O(n * k log k) O(n) For C++, the complexity is O(n * k log k), where n is the number of strings and k is the maximum length of a string. But for Python, the complexity is O(n * k) as there is no sorting involved.

Graph General

# Title Difficulty Approach Solution Time Space Note
399 Evaluate Division Medium โ€” Python, C++ O(N + M) O(N) N = number of equations, M = number of queries.

About

A collection of LeetCode problem solutions, rigorously tested with GoogleTest for C++ and pytest for Python.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Used by

Contributors

Languages