A clean, efficient environment for solving DSA problems with comprehensive testing.
-
Copy the template:
cp problems/template/solution.cpp problems/your_problem_name/solution.cpp
-
Edit the solution:
- Implement your solution in the
Solutionclass - Update test cases in
main() - Add problem description in the header comment
- Implement your solution in the
-
Compile and run:
cd problems/your_problem_name g++ -std=c++17 -o solution solution.cpp .\solution
DSA/
├── problems/ # All problem solutions
│ ├── template/ # Problem template
│ │ ├── solution.cpp # Solution template with test framework
│ │ ├── input.txt # Sample input file
│ │ └── output.txt # Expected output
│ └── second_largest/ # Example problem
│ └── solution.cpp # Implemented solution
└── README.md # This file
-
Comprehensive Testing Framework
- Easy-to-add test cases
- Detailed test output
- Support for different input types
-
Flexible Input
- Manual input mode
- File input support
- Predefined test cases
-
Documentation
- Problem description template
- Time/Space complexity analysis
- Key insights section
-
Using Predefined Tests:
- Add test cases in
main() - Run with:
g++ -std=c++17 -o solution solution.cpp && .\solution
- Add test cases in
-
Manual Testing:
- Uncomment
manualTest()inmain() - Run and enter custom input
- Uncomment
-
File Input:
- Uncomment
#define LOCAL_TEST - Add test cases to
input.txt - Run the program
- Uncomment
-
Create a new directory:
mkdir problems/your_problem_name cd problems/your_problem_name -
Copy the template:
cp ../template/solution.cpp . -
Start solving!
-
Prerequisites
- C++ compiler (g++, clang, or MSVC)
- C++11 or later
-
Getting Started
- Clone this repository
- Navigate to the project directory
- Compile and run the example:
cd problems g++ -std=c++11 second_largest.cpp -o second_largest .\second_largest
-
Copy the Template
cp problems/template.cpp problems/your_problem.cpp
-
Implement Your Solution
- For class-based solutions (recommended for most platforms):
class Solution { public: // Your solution method int solve(vector<int>& nums) { // Your code here } };
- For function-based solutions:
int solve(vector<int>& nums) { // Your code here }
- For class-based solutions (recommended for most platforms):
-
Using the Built-in Test Cases
- The template includes a
main()function with test cases - Add your test cases in the
main()function - Example:
int main() { Solution sol; // Test case 1 vector<int> test1 = {1, 2, 3, 4}; cout << "Test 1: " << sol.solve(test1) << endl; // Test case 2 vector<int> test2 = {5, 5, 5}; cout << "Test 2: " << sol.solve(test2) << endl; return 0; }
- The template includes a
-
Running the Tests
# Compile g++ -std=c++11 your_problem.cpp -o solution # Run .\solution
The template provides these helper methods:
// Reading input
int x = readInt(); // Read a single integer
long long y = readLong(); // Read a 64-bit integer
string s = readString(); // Read a string (space/newline separated)
vector<int> arr = readIntArray(); // Read N followed by N integers
vector<vector<int>> mat = read2DIntArray(); // Read rows, cols, then rows*cols integers
// Writing output
printArray({1, 2, 3}); // Prints: 1 2 3
print2DArray({{1,2}, {3,4}}); // Prints: 1 2\n3 4-
Problem Documentation
- Always include the problem statement at the top of your file
- Document input/output format
- Include constraints and examples
-
Code Style
- Use meaningful variable names
- Add comments for complex logic
- Keep functions small and focused
-
Testing
- Test edge cases (empty input, single element, etc.)
- Test with maximum input sizes
- Verify time and space complexity
See problems/second_largest.cpp for a complete example solution that finds the second largest distinct element in an array.
Feel free to contribute by adding more problems or improving the template. Make sure to follow the existing code style and include appropriate test cases.
- Testing
- Test edge cases (empty input, large inputs, etc.)
- Use the sample test cases from the problem statement
- Use
\ninstead ofendlfor faster output - Prefer
vectorover arrays - Use
unordered_mapwhen order doesn't matter - Reserve vector capacity when size is known in advance
- Fork the repository
- Create a new branch for your feature
- Commit your changes
- Push to the branch
- Create a new Pull Request
This project is open source and available under the MIT License.
- Standard Template Library (STL) documentation
- Competitive Programming resources