Search⌘ K
AI Features

LeetCode API Design

Learn to design a complex API for a coding platform like LeetCode. Understand how REST and GraphQL are combined to handle user registration, problem evaluations, contests, discussions, and interviews while meeting scalability, security, and low-latency requirements.

LeetCode API: Requirements and scope

LeetCode enables developers to sharpen programming skills by solving problems and verifying solution correctness. The API we design here covers problem-solving, code evaluation, programming contests with leaderboards, a discussion feature for sharing interview experiences and solutions, and an interview service for hiring.

Functional requirements

  • List questions: List practice problems filtered by difficulty level or topic.

  • Register for contests: Register for upcoming contests.

  • Submit code: Submit one or more solutions for evaluation during practice or contests.

  • Leaderboard: Fetch contest standings.

  • History: Track performance metrics, submitted solutions, and contest results.

  • Interviews: Conduct candidate interviews via the API.

Functional and nonfunctional requirements of LeetCode API design
Functional and nonfunctional requirements of LeetCode API design

Nonfunctional requirements

  • Availability: Must handle thousands of concurrent contest submissions.

  • Scalability: Support a growing user and contestant base.

  • Security: The primary concern is protecting the data processed by the API, not just transport-layer security. The API must accept only valid code payloads to prevent result manipulation.

  • Low latency: Minimize response times across all services.

Prerequisites

Several previously designed services serve as building blocks:

Design overview and decisions

With requirements established, the LeetCode API decomposes into five major services, each exposed as a direct endpoint. These services are interdependent: client requests for listing problems, registering for contests, submitting code, or commenting flow through an API gateway and fan out to the appropriate subservices. The following workflows detail each service's internal mechanics.

Working architecture of a LeetCode service
Working architecture of a LeetCode service

The components of the LeetCode service operate as follows:

Components and Services Details

Component or Service

Details

User Service

  • Deals with user's data
  • Handles requests related to user's registration and login

Problem Service

  • Handles requests related to practice problems
  • Communicates with other services for code execution requests
  • Maintains metadata of a user's performance

Contest Service

  • Performs contests related operations
  • Tackles requests such as contest registration, code submission, maintaining leaderboard, and so on

Interview Service

  • Handles scheduling of interview requests
  • Connects subservices, such as Zoom and the code execution service, for real-time communication and code evaluations

Discuss Service

  • Handles discuss queries
  • Connects with the pub-sub service to create topics and notify users

Cache

  • Caches responses to reduce number of calls to different endpoints

Workflow

...