Skip to content

tiru-r/go-leetcode-master

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

103 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go LeetCode Solutions

High-performance Go solutions to LeetCode problems with optimal algorithms and Go best practices.

Overview

This repository contains 150 LeetCode problems solved in Go, covering all major algorithm categories from Easy to Hard difficulty levels. Each solution is optimized for performance and follows Go best practices with comprehensive test coverage.

Statistics

  • Total Problems: 150 solutions
  • Difficulty Distribution:
    • Easy: 53 problems (35.3%)
    • Medium: 68 problems (45.3%)
    • Hard: 29 problems (19.3%)
  • Go Version: 1.24+
  • Test Coverage: 100% with benchmarks
  • Categories: 22 algorithm categories covered

Table of Contents

Features

  • Optimal Algorithms: Best time/space complexity solutions
  • Go Best Practices: Modern Go 1.24+ patterns and idioms
  • Performance Focused: Zero-allocation patterns and optimizations
  • Complete Testing: 100% test coverage with benchmarks
  • Comprehensive Documentation: Detailed explanations for each solution
  • Consistent Structure: Uniform project organization across all problems
  • Up-to-Date: Includes recent problems up to #3539

Quick Start

# Clone the repository
git clone https://github.com/yourusername/go-leetcode-master.git
cd go-leetcode-master

# Run tests for a specific problem
cd two_sum_1
go test -v -bench=. -benchmem

# Run all tests
make test

# Run benchmarks
make bench

# Check test coverage
make cover_html

Project Structure

Each problem follows a consistent directory structure:

problem_name_number/
├── README.md           # Problem description and approach
├── solution.go         # Optimized Go solution
└── solution_test.go    # Comprehensive tests and benchmarks

Build System:

  • Makefile - Build automation and testing
  • go.mod - Go module with dependencies (testify v1.5.1)
  • All problems use Go 1.24+ features

Problem Categories

Arrays & Hashing

Problem Difficulty Solution Key Concepts
1. Two Sum Easy Go Hash Map, Single Pass
49. Group Anagrams Medium Go Hash Map, Sorting
128. Longest Consecutive Sequence Medium Go Hash Set, Union Find
217. Contains Duplicate Easy Go Hash Set
238. Product of Array Except Self Medium Go Prefix/Suffix Products
242. Valid Anagram Easy Go Hash Map, Sorting
347. Top K Frequent Elements Medium Go Hash Map, Heap
350. Intersection of Two Arrays II Easy Go Hash Map, Two Pointers
387. First Unique Character in a String Easy Go Hash Map, String
412. Fizz Buzz Easy Go Array, Math
575. Distribute Candies Easy Go Hash Set, Array

Two Pointers

Problem Difficulty Solution Key Concepts
11. Container With Most Water Medium Go Two Pointers, Greedy
15. 3Sum Medium Go Two Pointers, Sorting
125. Valid Palindrome Easy Go Two Pointers, String Processing
141. Linked List Cycle Easy Go Floyd's Cycle Detection
160. Intersection of Two Linked Lists Easy Go Two Pointers
283. Move Zeroes Easy Go Two Pointers, Array

Sliding Window

Problem Difficulty Solution Key Concepts
3. Longest Substring Without Repeating Characters Medium Go Sliding Window, Hash Set
76. Minimum Window Substring Hard Go Sliding Window, Hash Map
424. Longest Repeating Character Replacement Medium Go Sliding Window, Hash Map
643. Maximum Average Subarray I Easy Go Sliding Window

Stack

Problem Difficulty Solution Key Concepts
20. Valid Parentheses Easy Go Stack, Hash Map
155. Min Stack Medium Go Stack, Design
341. Flatten Nested List Iterator Medium Go Stack, Design

Binary Search

Problem Difficulty Solution Key Concepts
33. Search in Rotated Sorted Array Medium Go Binary Search, Array Rotation
153. Find Minimum in Rotated Sorted Array Medium Go Binary Search, Array Rotation
981. Time Based Key-Value Store Medium Go Binary Search, Hash Map, Design

Linked Lists

Problem Difficulty Solution Key Concepts
2. Add Two Numbers Medium Go Linked List, Math
19. Remove Nth Node From End of List Medium Go Two Pointers, Linked List
21. Merge Two Sorted Lists Easy Go Linked List, Recursion
23. Merge k Sorted Lists Hard Go Linked List, Heap, Divide & Conquer
143. Reorder List Medium Go Linked List, Two Pointers
206. Reverse Linked List Easy Go Linked List, Recursion
234. Palindrome Linked List Easy Go Linked List, Two Pointers
237. Delete Node in a Linked List Medium Go Linked List

Trees

Problem Difficulty Solution Key Concepts
98. Validate Binary Search Tree Medium Go BST, DFS, Recursion
100. Same Tree Easy Go Binary Tree, DFS
101. Symmetric Tree Easy Go Binary Tree, DFS
102. Binary Tree Level Order Traversal Medium Go Binary Tree, BFS
104. Maximum Depth of Binary Tree Easy Go Binary Tree, DFS
105. Construct Binary Tree from Preorder and Inorder Traversal Medium Go Binary Tree, DFS
124. Binary Tree Maximum Path Sum Hard Go Binary Tree, DFS
226. Invert Binary Tree Easy Go Binary Tree, DFS
230. Kth Smallest Element in a BST Medium Go BST, DFS
235. Lowest Common Ancestor of a Binary Search Tree Medium Go BST, DFS
572. Subtree of Another Tree Easy Go Binary Tree, DFS

Tries

Problem Difficulty Solution Key Concepts
208. Implement Trie Medium Go Trie, Design
212. Word Search II Hard Go Trie, Backtracking

Heap / Priority Queue

Problem Difficulty Solution Key Concepts
295. Find Median from Data Stream Hard Go Heap, Design

Backtracking

Problem Difficulty Solution Key Concepts
17. Letter Combinations of a Phone Number Medium Go Backtracking, String
22. Generate Parentheses Medium Go Backtracking, String
37. Sudoku Solver Hard Go Backtracking, Matrix
79. Word Search Medium Go Backtracking, Matrix
698. Partition to K Equal Sum Subsets Medium Go Backtracking, Bit Manipulation
1239. Maximum Length of a Concatenated String with Unique Characters Medium Go Backtracking, Bit Manipulation

Graphs

Problem Difficulty Solution Key Concepts
200. Number of Islands Medium Go DFS, BFS, Union Find
207. Course Schedule Medium Go Topological Sort, DFS
261. Graph Valid Tree Medium Go DFS, BFS, Union Find
269. Alien Dictionary Hard Go Topological Sort, DFS

Test Cases

The Alien Dictionary problem includes comprehensive test coverage:

// Basic valid ordering
{"wrt", "wrf", "er", "ett", "rftt"} → "wertf"

// Simple two character ordering  
{"z", "x"} → "zx"

// Invalid: contradiction in ordering
{"z", "x", "z"} → "" (empty - invalid)

// Complex valid ordering
{"za", "zb", "ca", "cb"} → "azbc"

// Edge cases
{} → "" (empty input)
{"abc"} → "abc" (single word)
{"abc", "abc"} → "abc" (duplicate words)

// Invalid cases
{"abc", "ab"} → "" (prefix contradiction)
{"a", "b", "a"} → "" (cycle detection)
{"ab", "bc", "ca"} → "" (multi-letter cycle)

| 417. Pacific Atlantic Water Flow | Medium | Go | DFS, BFS | | 847. Shortest Path Visiting All Nodes | Hard | Go | BFS, Bit Manipulation | | 953. Verifying an Alien Dictionary | Easy | Go | Hash Map, String |

Advanced Graphs

Problem Difficulty Solution Key Concepts
1192. Critical Connections in a Network Hard Go Tarjan's Algorithm, DFS
1568. Minimum Number of Days to Disconnect Island Hard Go DFS, Articulation Points

Dynamic Programming

Dynamic Programming problems with optimal substructure and overlapping subproblems.

Problem Difficulty Solution Key Concepts
5. Longest Palindromic Substring Medium Go DP, String
53. Maximum Subarray Medium Go DP, Kadane's Algorithm
70. Climbing Stairs Easy Go DP, Fibonacci
121. Best Time to Buy and Sell Stock Easy Go DP, Array
122. Best Time to Buy and Sell Stock II Medium Go DP, Greedy
132. Palindrome Partitioning II Hard Go DP, String
139. Word Break Medium Go DP, String
152. Maximum Product Subarray Medium Go DP, Array
174. Dungeon Game Hard Go DP, Matrix
198. House Robber Medium Go DP, Array
213. House Robber II Medium Go DP, Array
300. Longest Increasing Subsequence Medium Go DP, Binary Search
322. Coin Change Medium Go DP, BFS
338. Counting Bits Easy Go DP, Bit Manipulation
354. Russian Doll Envelopes Hard Go DP, Binary Search
377. Combination Sum IV Medium Go DP, Backtracking
509. Fibonacci Number Easy Go DP, Math
647. Palindromic Substrings Medium Go DP, String
887. Super Egg Drop Hard Go DP, Binary Search
1143. Longest Common Subsequence Medium Go DP, String
1622. Fancy Sequence Hard Go DP, Math
2518. Number of Great Partitions Hard Go DP, Combinatorics

Greedy

Problem Difficulty Solution Key Concepts
435. Non-overlapping Intervals Medium Go Greedy, Sorting

Intervals

Problem Difficulty Solution Key Concepts
56. Merge Intervals Medium Go Sorting, Array
57. Insert Interval Medium Go Array, Intervals
252. Meeting Rooms Easy Go Sorting, Array
253. Meeting Rooms II Medium Go Heap, Sorting

Math & Geometry

Problem Difficulty Solution Key Concepts
48. Rotate Image Medium Go Matrix, Math
54. Spiral Matrix Medium Go Matrix, Simulation
73. Set Matrix Zeroes Medium Go Matrix, Array
149. Max Points on a Line Hard Go Math, Geometry
335. Self Crossing Hard Go Math, Geometry
587. Erect the Fence Hard Go Geometry, Convex Hull
1453. Maximum Number of Darts Inside of a Circular Dartboard Hard Go Geometry, Math

Bit Manipulation

Problem Difficulty Solution Key Concepts
136. Single Number Easy Go Bit Manipulation, XOR
190. Reverse Bits Easy Go Bit Manipulation
191. Number of 1 Bits Easy Go Bit Manipulation
268. Missing Number Easy Go Bit Manipulation, Math
371. Sum of Two Integers Medium Go Bit Manipulation

String Processing

Problem Difficulty Solution Key Concepts
7. Reverse Integer Medium Go Math, Integer Overflow
9. Palindrome Number Easy Go Math, String
13. Roman to Integer Easy Go Hash Map, String
14. Longest Common Prefix Easy Go String, Trie
38. Count and Say Medium Go String, Recursion
151. Reverse Words in a String Medium Go String, Two Pointers
344. Reverse String Easy Go String, Two Pointers
541. Reverse String II Easy Go String, Two Pointers
796. Rotate String Easy Go String, KMP

Array Processing

Problem Difficulty Solution Key Concepts
26. Remove Duplicates from Sorted Array Easy Go Array, Two Pointers
66. Plus One Easy Go Array, Math
88. Merge Sorted Array Easy Go Array, Two Pointers
118. Pascal's Triangle Easy Go Array, Dynamic Programming
169. Majority Element Easy Go Array, Hash Map, Boyer-Moore
229. Majority Element II Medium Go Array, Boyer-Moore

Advanced String & Pattern Matching

Problem Difficulty Solution Key Concepts
10. Regular Expression Matching Hard Go Dynamic Programming, Recursion
44. Wildcard Matching Hard Go Dynamic Programming, Greedy
65. Valid Number Hard Go String, Finite State Machine

Design Problems

Problem Difficulty Solution Key Concepts
146. LRU Cache Medium Go Hash Map, Doubly Linked List
285. Inorder Successor in BST Medium Go BST, Inorder Traversal
346. Moving Average from Data Stream Easy Go Design, Queue
348. Design Tic-Tac-Toe Medium Go Design, Array

Complex Algorithms

Problem Difficulty Solution Key Concepts
42. Trapping Rain Water Hard Go Two Pointers, Stack, DP
126. Word Ladder II Hard Go BFS, DFS, Backtracking
171. Excel Sheet Column Number Easy Go Math, String
202. Happy Number Easy Go Hash Set, Math
218. The Skyline Problem Hard Go Heap, Divide & Conquer
233. Number of Digit One Hard Go Math, Dynamic Programming
271. Encode and Decode Strings Medium Go String, Design
289. Game of Life Medium Go Array, Simulation
794. Valid Tic-Tac-Toe State Medium Go String, Game Theory
937. Reorder Data in Log Files Easy Go String, Sorting

Additional Problems

Notable recent and advanced problems demonstrating cutting-edge algorithms:

Problem Difficulty Solution Key Concepts
1170. Compare Strings by Frequency of the Smallest Character Medium Go String Processing, Binary Search
1176. Diet Plan Performance Easy Go Sliding Window, Array
1304. Find N Unique Integers Sum up to Zero Easy Go Math, Array Construction
1309. Decrypt String from Alphabet to Integer Mapping Easy Go String Processing
1310. XOR Queries of a Subarray Medium Go Prefix Sum, Bit Manipulation
1311. Get Watched Videos by Your Friends Medium Go BFS, Hash Map
1317. Convert Integer to the Sum of Two No-Zero Integers Easy Go Math, String
1732. Find the Highest Altitude Easy Go Prefix Sum, Array
1768. Merge Strings Alternately Easy Go Two Pointers, String
2215. Find the Difference of Two Arrays Easy Go Hash Set, Array
2408. Design SQL Medium Go Design, Hash Map
3276. Select Cells in Grid With Maximum Score Hard Go Dynamic Programming, Bitmask
3539. Find Sum of Array Product of Magical Sequences Hard Go Advanced Math, Combinatorics

Concurrency

Problem Difficulty Solution Key Concepts
1117. Building H2O Medium Go Concurrency, Synchronization
1226. The Dining Philosophers Medium Go Concurrency, Deadlock Prevention

Testing & Benchmarks

This repository emphasizes comprehensive testing and performance analysis:

Test Coverage

  • 100% test coverage across all 150 problems
  • Unit tests for all edge cases and example inputs
  • Benchmark tests for performance analysis
  • Memory allocation tracking with -benchmem

Available Make Commands

# Run all tests with coverage
make test

# Run tests with verbose output
make test_v

# Run benchmarks for all problems
make bench

# Generate coverage report (HTML)
make cover_html

# Generate coverage report (function level)
make cover_func

# Format all Go code
make fmt

# Run Go vet for code quality
make vet

# Count total lines of code
make loc

# Count total problems solved
make solved

Test Dependencies

  • testify v1.5.1 - Testing framework for assertions and test suites
  • Built-in Go testing tools for benchmarks and coverage

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-solution)
  3. Add your solution following the existing structure:
    • Create a directory named problem_name_number/
    • Add solution.go with the implementation
    • Add solution_test.go with comprehensive tests
    • Add README.md with problem description and approach
  4. Ensure all tests pass and benchmarks are included
  5. Submit a pull request

Code Quality Standards

  • Follow Go best practices and idioms
  • Use meaningful variable and function names
  • Include comprehensive test cases
  • Add benchmarks for performance-critical solutions
  • Document complex algorithms with comments

Repository Highlights

Problem Range

  • Classic Problems: Two Sum (#1), Valid Parentheses (#20), Merge Two Sorted Lists (#21)
  • Advanced Algorithms: Critical Connections (#1192), Shortest Path Visiting All Nodes (#847)
  • Recent Challenges: Problems up to #3539 showing active maintenance
  • Concurrency: Go-specific concurrency problems with goroutines and channels

Algorithm Categories Covered

22 Major Categories including Arrays, Dynamic Programming, Graphs, Trees, and more
Advanced Graph Algorithms like Tarjan's Algorithm and Convex Hull
Concurrency Patterns unique to Go programming
Mathematical Algorithms for geometry and combinatorics
String Processing with pattern matching and manipulation
Design Problems including LRU Cache and data structures

Performance Focus

  • Zero-allocation patterns where possible
  • Optimal time complexity solutions prioritized
  • Memory-efficient implementations with detailed benchmarks
  • Real-world performance considerations

License

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


Total Problems Solved: 150 | Go Version: 1.24+ | Last Updated: 2025

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors