LeetCode is a popular platform designed to help software engineers prepare for technical interviews. It features a large collection of coding problems across various difficulty levels, allowing users to practice everything from basic concepts to advanced algorithms.
The platform includes an online code editor with support for multiple programming languages, and automatically evaluates submissions using hidden test cases to provide instant feedback. In addition to practice, LeetCode hosts regular timed contests where users solve problems under pressure and compete on live leaderboards, simulating real interview conditions.
Functional Requirements
- Users can browse and filter a list of coding problems (e.g., by difficulty or tags).
- Users can view detailed problem descriptions, including constraints and starter code for multiple languages.
- Users can write and submit code solutions, and receive feedback based on test case execution.
- Users can see a real-time leaderboard during competitions, ranked by performance.
Non-Functional Requirements
- The system should prioritize availability; minor inconsistencies (e.g., leaderboard delays) are acceptable.
- Code execution must be isolated and secure, preventing misuse of system resources.
- Submission results should be returned within a few seconds (target: ≤5 seconds).
- The system should scale to handle high concurrency, especially during competitions (up to 100,000 users).
Data model design
To support the key features, we can identify the following entities:
- Problem: Represents each coding question, including its description, constraints, test cases, and expected outputs.
- Submission: Captures a user’s code submission along with execution results against test cases.
- Leaderboard: Maintains rankings during competitions based on user performance.
API Design
The system can be cleanly modeled using a RESTful API. Given the interaction patterns—browsing problems, submitting code, and viewing leaderboards—REST is sufficient and keeps the design simple. Real-time protocols like WebSockets are unnecessary here, since leaderboard updates can tolerate slight delays and be refreshed via periodic polling (e.g., every 5 seconds).
1. List Problems
GET /problems?page=1&limit=20 -> Problem[]
Returns a paginated list of problem summaries. Each item includes basic metadata such as problemId, title, difficulty, and tags. Full problem details are excluded to reduce payload size.
2. Get problem detail
GET /problems/:id?language={language} -> Problem
Returns the full problem description, constraints, and a language-specific code template. The language parameter is optional and defaults to a predefined language (e.g., Python).
3. Submit Code Solution
POST /problems/:id/submit -> Submission { code: string, language: string }
We don’t pass userId from the client because client input is not trustworthy and can be easily spoofed. Instead, we use JWT for authentication. The server verifies the token and extracts the userId from it, ensuring that the identity is secure and cannot be tampered with.