Coding Competition Platform Design: Interview with a FAANG Engineer

Coding Competition Platform Design

Watch someone solve the design a coding competition platform problem in an interview with a FAANG engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.

Video

Interview Summary

Problem type

Design a Coding Competition Platform

Interview question

Design a system similar to a well-known competitive programming platform. Users can join timed 90-minute competitions, submit code solutions to multiple problems, and receive scores based on automated test case execution. The system must update a real-time leaderboard as submissions are evaluated, support up to 500K users joining a single contest, and handle peak submission throughput of 50,000 submissions per minute - all while safely sandboxing untrusted user code.

Read more about the questions

Interview Feedback

Feedback about Magnetic Hare (the interviewee)

Advance this person to the next round?

Yes

How were their technical skills?

3/4

How was their problem solving ability?

3/4

What about their communication ability?

4/4

Strengths and What Went Well Clear communication: Communicated the thought process clearly and maintained a structured approach throughout the interview.

Requirements clarification: Asked relevant clarifying questions to define the functional requirements and establish the scope before proceeding with the design.

Receptive to feedback: Demonstrated a collaborative mindset by being receptive to interviewer feedback and effectively incorporating suggestions into the solution.

Areas for Improvement

Non-functional requirements: The NFR discussion could have been more comprehensive. Important aspects such as scalability, latency, reliability, fault tolerance, and consistency vs. availability trade-offs should be identified and discussed proactively.

Capacity estimation: Back-of-the-envelope calculations should focus on reasonable approximations rather than precise numbers. The objective is to use estimates to validate architectural decisions around throughput, storage, memory, and infrastructure capacity.

Sandboxing and isolation: Where applicable, consider security boundaries, workload isolation, sandboxing, and blast-radius reduction as part of the architecture rather than treating security as an afterthought.

Data structure selection: Select data structures based on the system's access patterns and clearly explain the rationale and trade-offs. For example, Redis Sorted Sets are well suited for leaderboard use cases requiring efficient score updates, ranking, and top-K retrieval.

Advice for Future Interviews

Practice more end-to-end system design problems under realistic interview time constraints to improve both breadth and technical depth.

Refresh core distributed systems concepts, particularly scalability, partitioning, caching, consistency models, availability, fault tolerance, asynchronous processing, and failure recovery.

Strengthen familiarity with commonly used data structures, databases, caches, queues, and infrastructure technologies, and understand which system requirements and access patterns make each appropriate.

Establish a simple working architecture first and progressively deepen the design. For every major decision, practice explaining: Requirement → Design Choice → Rationale → Alternatives → Trade-offs.

Feedback about Verdant Goblin (the interviewer)

Would you want to work with this person?

Yes

How excited would you be to work with them?

4/4

How good were the questions?

4/4

How helpful was your interviewer in guiding you to the solution(s)?

4/4

Interview Transcript

Magnetic Hare: Uh, hello? Hello. Uh, hey, yeah, can you hear me?

Verdant Goblin: Yes, I can hear you.

Magnetic Hare: Awesome, great. Thanks for taking the time.

Verdant Goblin: Thanks for joining.

Magnetic Hare: Yeah, thank you.

Verdant Goblin: All right, um, can you talk a little bit about yourself, like especially like what kind of interviews are coming along? What kind of a company, how much of, uh, how much of your experience you have. Yeah. And, uh, what type of a domain you are interviewing for.

Magnetic Hare: Yeah. Uh, I can give a little context. So, um, yeah, so I'm, I have basically screen interviews upcoming for, uh, mostly consumer companies. So, um, [REDACTED], [REDACTED], um, uh, there I have a [REDACTED] interview as well. And my experience is primarily as a full-stack engineer. I've worked with multiple companies, both in big tech and small, for almost a decade at this point.

Verdant Goblin: Mm-hmm.

Magnetic Hare: I haven't been interviewing, you know, for a long time. So this is the basically trying to, me trying to shake my rust off a little bit. So, but yeah, I'm targeting primarily product-facing roles. Uh, in a full-stack space. Um, I'm more comfortable on the front end, but I'll work on backend, um, as well too. And, uh, yeah, timeline. Yeah, these interviews are coming up very soon, so definitely want to get as much practice reps in and figure out where I can improve.

Verdant Goblin: All right, that's a great context and good to know that. Um, and today's interview is going to be system design. Um, Do you know like how the structure is, or do you want me to clarify before we get into the actual interview?

Magnetic Hare: Yeah, I've been studying with like [REDACTED] and Alex Xu's book. I haven't gone through every single problem, but I think I have at least some fundamentals. So, um, yeah, I think we can probably just get into it unless you have more questions.

Verdant Goblin: Okay, awesome. So, uh, I think this is 12 years of experience, so my level of expectation would be quite different, right, because based on the levels as well. So I'll start with the very ambiguous statement, and probably I expect the, the learner to decompose that ambiguous statement into different functional requirements by asking clarifying questions. Is that okay? Yeah, that sounds perfect. Okay, today, uh, you're going to design a coding competition platform. Okay. Similar to [REDACTED].

Magnetic Hare: Yep. Okay, um, yeah, this is a great question. Should we move over to the whiteboard, or, uh, do, do you want to capture the requirements here in the text?

Verdant Goblin: Whatever you feel comfortable, I'm fine with that.

Magnetic Hare: Okay, yeah, let's switch over to the whiteboard. Design coding platform like [REDACTED]. Yeah, so yeah, this sounds like a very interesting problem. Definitely have a couple questions because I know [REDACTED] has so many different features. So functional requirements and scope. So I imagine when you think about a coding platform and competitions, are we designing a specific competition where there's a set of problems or one problem, or is this the general practice platform where users can select arbitrary number of problems to solve?

Verdant Goblin: Well, it's an arbitrary number of problems. A competition can run for 90 minutes. Right? In that 90 minutes, people can submit the code as much as they want. And after the submission, they will be rated based on the test case execution. And I think every problem they submit, their score will be calculated for each of those persons. And then we need to calculate the leaderboard also. We need to show the leaderboard. top 10 users of the leaderboard or something like that.

Magnetic Hare: I see. So the scores you mentioned for the submission, is the final score at the end of the competition basically the highest score that the user has achieved in that session? I imagine if they run multiple times, they might receive different scores if they introduce a bug, for example.

Verdant Goblin: Yeah, say for example, like within the 90 minutes, say uh, there is like, uh, 10 problems in a competition, right? And the user can submit each of those problems. As soon as they submit, the leaderboard, as you know, as soon as like out of 10 problems I have submitted, uh, 2 problems, then I will have some kind of a score. And similarly, you would have submitted some 4 or 5 problems at that time, your score will be different. Okay, uh, I think it has to reflect the leaderboard in real time who is leading in the competition.

Magnetic Hare: I see, so real-time updates, uh, yep, for, um, who is leading. Okay, um, all right. And then, yeah, I think maybe just some more questions on the leaderboard. You mentioned real-time updates while leading. Is this something that they see while they're in the competition, or is that a different page essentially that they're looking at. And do users also need to know basically their own rank? There might be millions of people in these competitions. Do they need their own rank as well indicated as part of the score?

Verdant Goblin: I think the individual rank can be— probably it's out of scope for now, right? I think we can discuss, but I'm more focused on displaying the leaderboard, uh, right? Irrespective, as soon as the user logs in, uh, they should be able to see the leaderboard for that competition.

Magnetic Hare: Okay, okay. So the leaderboard is updated in real time but available on logging, uh, in, uh, as a separate, as a separate widget, for example, on the page. So, um, I think that makes sense. I, I I think I'm just gonna go for these particular requirements to start. Let me, let me condense these down since I was taking notes. But the TL;DR is users can join competitions and solve problems by submitting code submissions. Users receive a score at the end of the competition to simplify which affects their rating.

Verdant Goblin: Well, that— I think I just want to be, uh, we are on the same page. Um, the score will be calculated as soon as they submit individual problem for that competition, right? Uh, I, I think that we don't need to end the competition to know the score. Uh, as soon as they submit that problem, their score will be cumulatively calculated and that will be in the leaderboard.

Magnetic Hare: I see. Okay. And used— yeah, so basically they'll receive a score at the submission of the problems and leaderboard is updated in near real time basically as the users are submitting problems, correct?

Verdant Goblin: Yes.

Magnetic Hare: All right. Okay. And okay, so I think, yeah, this is, I think, a good scope of just you know, this is a good meaty scope of what we can go after. I think there's many other things that we could also do, maybe like seeing historical, you know, historical submissions, you know, live replays of maybe high scores. But I think this is already a lot to go through. So let's maybe just focus on these.

Verdant Goblin: Yeah.

Magnetic Hare: And then I think we can talk about the nonfunctional requirements here as well. Thinking about this as a user, I want to make sure first off that my submissions are durable. It wouldn't make sense if I submit, you know, code and for some reason my score changes because the server doesn't know what my latest submissions are. So I think every code submission must be durably backed by our server so we don't lose progress as users are submitting. This is a different requirement than the leaderboard itself. So I think for the leaderboard, you mentioned real time. That makes a lot of sense that the leaderboard itself can be eventually consistent. So because near real time implies some delays and users don't necessarily need an update on leaderboard instantly. I think then scale as well. So I'm trying to think of how many users are going to be using this at the same time. And then maybe one more question for the competitions themselves. Are competitions defined by basically like admins of [REDACTED], or are those, um, we can assume there's some other system that's already generating these competitions?

Verdant Goblin: Yeah, there will be some admin who will add those competitions and problems, uh, right? I, I think let's keep that out of scope for this particular exercise, but, uh, assume that the data is already available.

Magnetic Hare: Okay, so yeah, okay, that sounds good. And then I'm trying to think of how the code submits itself as well too. I think isolation of the code submissions is like an important safety feature. So from a safety security perspective, we don't want people who, you know, maybe they try to hack into our own systems. So user submissions should be run in a sandbox environment. So to protect our own, our own systems, servers. And then there might be other aspects of this, like, uh, too, for, you know, rate limiting, uh, and things like that. But, um, I think this is a good set of non-functional requirements. I, I think one thing as well, um, you know, as I'm thinking about using the feature itself Submitting the scores and getting a result back should be relatively low latency. I don't think it would be acceptable if our service took, you know, 10 minutes to evaluate submissions because they only have 90 minutes to do those things anyway. So I'm also going to add in low latency for code submissions, and let's define what low latency actually means. So I'm not sure how complex these problems are, but I think we definitely should receive some answer in less than 30 seconds for P99. Usually I feel [REDACTED] is very, very fast. I'm hoping, you know, P50 at least is less than 5 seconds since the actual data itself is maybe like a kilobyte at most and hopefully we already have machines ready to, um, to run this code. And so yeah, I think— okay, is there any other requirement you think I'm missing here, or do you think this is a good—

Verdant Goblin: Um, I think assume that, uh, the write throughput, right, uh, at peak there could be 50,000 code submissions per minute. Great point, right? Um, yeah, the daily active users can be 10 million, and, uh, That could be 500K users may join a single 90-minute contest.

Magnetic Hare: 500K users in any single contest. And I'm sorry, the first thing you mentioned, did you say something about the concurrent users? So, or I think 500K users in any single contest. Could there be multiple contests happening at the same time?

Verdant Goblin: Yeah, yeah, yeah. It can happen multiple. But at peak, like, assume that 50,000 per minute.

Magnetic Hare: 50K submissions per second or per minute?

Verdant Goblin: Per minute. Per minute.

Magnetic Hare: Okay. All right. So I think this is definitely useful to know in terms of scale. Before I leverage any of these numbers in my design, I want to just go through what the core entities might be so that we can talk through exactly what data we're talking about as things are flowing. I want to get a high-level design working as well, and then I think then we can talk through, okay, what does this really mean if there's 500K users, and what technology choices might that represent? So I'll come back to this piece.

Verdant Goblin: Okay.

Magnetic Hare: Real quick, just to get us on the same page, so in terms of the core entities, we have the users, We have competitions. The competitions have problems, so problem definitions. And yeah, so basically just the problem definitions. I would say we have test cases as well. We can think through maybe how to define these, but test cases for particular problems. Users also have submissions, so the raw code submitted by users. And then in terms of the leaderboard, I'm trying to think through whether we should put it on the users. But when you think about it, the comp— the leaderboard is specific to the competition, right? It's not like a like a score for the user. Am I understanding that correctly?

Verdant Goblin: That's right. So, but you may want to keep it as a separate entity because that, that is a different service, right? That probably we can discuss more in the deep dive, but, um, yeah, that's up to you.

Magnetic Hare: Yeah, I'm, I'm thinking right now that leaderboard can be a separate entity entirely, and we can get into the table, um, as well, but This should host the scores for users as they are going through competition. So I think this should solve at least all the data that we need. I'm just going to give the functional requirements one more look. Yes, we have users, competitions, problems, test cases, submissions that run, and then yeah, leaderboard. which will give us our data. So yeah, I think this is, I think this is good. So let's just keep moving on to the high-level API. So let's just go through our functional requirements. So the TL;DR, users can join competitions and solve. So let's just start with that, just joining a competition. So when we're joining a competition, I'm assuming, you know, not all of, not every user is going to join a competition, but just to create an entry that's someone has joined a competition.

Verdant Goblin: Mm-hmm.

Magnetic Hare: We can make a POST— we can make— we can use RESTful API to join a particular competition. And we will address which particular competition that we're joining within the URL itself. I don't think there's much need for a, like, I don't think we need much here. We just— we're just trying to indicate that a user has joined. So, you know, 200 for joining, user tokens, user ID will be identified through the token on this request. And we don't need to— we don't need to do too much else. So this just indicates that someone's joined a competition. From there, we'll want to understand the problem definitions that are associated with each competition. So for this, let's just put it on the, uh, competition endpoint itself. And what we should get back is the list of the problems that we, uh, talked about earlier. And so this, this will now render, you know, maybe there are 7 problems and so on. Um, And yeah, so user then will basically type in— so I'm thinking through, you know, if I'm on [REDACTED], I'm going to type in my different, uh, you know, um, the solutions to those problems. And once I actually am done, I'll submit, uh, to that competition.

Verdant Goblin: Now, quick question there, like in the, uh, get retrieving the list of problems Uh, how do you organize this data in the UI? Say a competition might have 100 problems, right? Um, and do you— how do you want to do that? And what kind of, uh, what are the different options you have to address those, uh, user experience?

Magnetic Hare: Yeah, that's a great question. I didn't think through— I've never done a [REDACTED] competition, but I think with 100 problems, that's quite a bit. So At a high level, one thing that you could do could be incorporate pagination here. So, we have a cursor and a limit.

Verdant Goblin: Mm-hmm.

Magnetic Hare: We can basically fetch just maybe 5 problems to start. And then as the user is completing those problems or they want to skip problems, then they can use the cursor and the limit to paginate through that. And from the UI perspective, we'll just load maybe the first 5 and have buttons to paginate through more problems and progressively fetch as needed. I don't think we need to do something really fancy like infinite scroll or something like that.

Verdant Goblin: Mm-hmm.

Magnetic Hare: But this would at least allow us to fetch many problems, make it quick to see them, but not overload.

Verdant Goblin: Yeah, so, um, what are the other pagination— what are the pros and cons and when do you use what?

Magnetic Hare: Yeah, so I think in this case competitions are set in stone, so a cursor would be better if, uh, you know, we tend to use cursors at work, uh, to make sure a changing list underneath the hood is still stable. So as more data comes in or data gets deleted, a cursor enables a user to have a stable point in time of that changing underlying dataset. We could also just use page numbers as well, because since the competition's defining the problems in advance, there's no possibility for the competition to change while it's in progress. So Page numbers would have worked completely fine here too without much difference. The only thing that is nice with cursors and limits is that we can change those as needed if, let's say, we deployed— you can change page numbers as well too, but the client can kind of pick how much they want without encoding page size up front.

Verdant Goblin: Okay.

Magnetic Hare: So, all right, so let's, let's keep moving. Posting for the competition, I think we're getting to the, the, the problem itself. So each competition has certain problems, and I will submit my, my problem code itself. I'm trying to think through like what response we can get back. I think we can basically just send back, we might want to think of like a different endpoint or a core entity here as well. I'm basically trying to think through like the results since the result is part of the score that we're going to be including. So I think what we can do is just have a results entity as well here too.

Verdant Goblin: Mm-hmm.

Magnetic Hare: body contains submission code. And yeah, I think, I think this, this gets us through the, the competition code. The one thing I didn't call out here is basically an error for if the competition's out of time, which I think we can, we can get to that piece as well too. But someone might try to submit problem submission after time has already expired, and then we don't want to enable them to update anything anymore. And we can get into how to stop people from doing that as well too. And then finally, this GET. So, yeah, so real-time updates of the leaderboard itself, I think we have multiple options, so I can you know, maybe talk about a trade-off. We could add a GET endpoint for the leaderboard for a particular competition, but one of the downsides of using GET is that if we wanted that to update in near real time, we would actually need to continuously ping our servers. And so I'm thinking a different way of getting that data would be better. And, and so one way that we can do that is Thinking about this interaction, we're not actually sending anything up front. We're actually just receiving updates to the leaderboard. So it's mostly the server responding multiple times. One good way to handle this would be to use server-side events where we hit the endpoint for leaderboards and we leverage server-side events for continuous updates of how the leaderboards changing. That way we don't need to reestablish the connection multiple times to get those updates.

Verdant Goblin: Right, so in short, like, what kind of a connection you'll use for leaderboard? Like, what protocol?

Magnetic Hare: SSC travels over HTTP from my understanding, so it's not completely changing the connection the way WebSockets would, where we would upgrade the connection to A WebSocket connection. So I believe it follows the request header when you hit the leaderboard endpoint for a particular leaderboard, and that'll establish a persistent connection with our server for us to send back the updates for, for the leaderboard changes, basically without needing to reestablish the connection at all. Okay, so get leaderboard for a particular competition, SSE connection. And what this will give us back— there's many ways to do this.

Verdant Goblin: Are you typing? Sorry, are you typing? Because I don't see— sometimes it happens. I need to refresh the screen.

Magnetic Hare: Oh yeah, sorry, let me, let me check this. Are you able to see my— yes.

Verdant Goblin: I see your cursor, but I don't see after that 2 APIs. I don't see that 3 APIs right after POST.

Magnetic Hare: That's weird. Maybe let me try to refresh real quick. Let's see.

Verdant Goblin: Yeah, I think I need to refresh as well. This happens in the interview. I'll, I'll refresh. I'll refresh.

Magnetic Hare: Okay.

Verdant Goblin: All right, uh, now I can see.

Magnetic Hare: Okay, yeah, I'm trying to write— it lost my updates as well too, actually. So, ah, okay, so this is a get leaderboard competition and, um, SSC events sent back. Um, and we didn't talk too much on, um, like, I, I'm just assuming we're gonna go with, uh, should we just go with top 50 here, or do you want a specific, um, format for the leaderboard?

Verdant Goblin: Um, say that, uh, top, uh, X, but X can be configured, right?

Magnetic Hare: Okay, okay. So, uh, top number We can also do the same thing here with pagination. Oops. We'll just put pagination as a placeholder. But it could be the same thing for cursors and all that.

Verdant Goblin: Mm-hmm.

Magnetic Hare: So, each SSE update will actually encode the latest leaderboard update. And so, basically, this will be a list of users. Leaders is users. And yeah, I think this, this does it for the high-level API. We can both join a competition, we can see the problems, we can submit submissions to those problems. I think one thing that's kind of missing here is like what happens as the user is in the competition itself and we end up needing to— we end up needing to close the— basically close the connection at that point so that they don't need this. And so I think thinking through that, I might want to make some changes to the API even here. Which is instead of actually posting, I— there's actually no reason why this couldn't also be on a WebSocket connection itself as well. And so let me just make some updates to this API because I think I'll need to do this anyway when thinking about how a competition ends. So WebSocket endpoints, user to server, send submission. which basically encodes this thing that we talked about. And then for user—

Verdant Goblin: I'm just curious, like, why do we need WebSocket for submission?

Magnetic Hare: The thing that I was trying to get around for this is basically how do you tell the user that you're done? In that case, you actually do need like some sort of, uh, uh, you need some sort of signal from, from the server. You could go either, either way here. I'm trying to basically cut down on the number of, uh, components, uh, here. Uh, if you think there— the reason I don't want to use the client clock is the client can always change whatever, um, clock it is. Um, but I want to basically stop the competition, um, driven by the server update as as that's happening. And there's some other benefits with that too. Like you could even get features like, hey, there's like, I don't know, 100 other people also working on this competition right now.

Verdant Goblin: Right. So that will be like as soon as the user submits the code, right, the server will update. It has to execute those test cases. Once after running all those test cases, it will just do the scoring, right?

Magnetic Hare: Right.

Verdant Goblin: I think the user, as soon as the score is updated, probably the user will update— the submission status will be updated as soon as they submit the code. After a few minutes, I think once they calculate that time, it will update the status back to that UI. Yeah, I think that's fine. We can use WebSocket as one of the things, but I feel that It's, um, it's a huge undertaking, right? Uh, especially with respect to infrastructure complexity.

Magnetic Hare: Oh yeah.

Verdant Goblin: Uh, maintaining that WebSocket, is it, uh, that's a trade-off whether do we really need for the use case, but that's something we can discuss when we go to that high-level architecture.

Magnetic Hare: That's fair. You know, maybe I'm over-optimizing to start. So let me just go back to that REST API and then we can talk through like, there's many ways to actually, uh, go about this. So let me just kind of put this back to where it was and then We can keep going. Yes, I think it was right here. So, let me just get to the high-level API at this point because, yeah, we're kind of far into this and I still haven't gotten everything going. So, we'll have our users that are joining— oops. Joining competitions. So, just call this the competition service. This competition service will be stateless with our current API.

Verdant Goblin: Mm-hmm.

Magnetic Hare: Both— and basically service all of our REST APIs that we currently have. And we'll have a— basically a database. I'm gonna default to Postgres right now and we can talk through why this is okay. But let's just keep it at Postgres for now. And let's talk through the different dataflows. And so the database has a couple different tables for each of those entities that we mentioned, but I'm going to get into the, the data modeling in a bit after I get everything in. At least what we have right now should be able to solve, you know, join a competition and get the problem definitions.

Verdant Goblin: Mm-hmm.

Magnetic Hare: The piece that it doesn't solve is how exactly we handle the submission of the problems themselves. And so this is where, you know, not having run through this before, there's likely going to be multiple types of languages with different needs for like what type of sandbox environment to run in. Like you might be running in Java, you might be running in Ruby, in Python, whatever it is. One thing that I want to protect against whatever is handling the submission of the users from them submitting is basically to decouple those 2 services so that we don't— we can avoid slamming the services that are actually— the servers that are going to be checking the code against the test cases from people submitting them.

Verdant Goblin: Mm-hmm.

Magnetic Hare: I'm gonna jump right into that because I think that piece is a little bit more interesting. But one way that we can basically do that decoupling that I mentioned was when we— when a user submits their submission, we'll persist the record for that submission into Postgres.

Verdant Goblin: Mm-hmm.

Magnetic Hare: And as soon as we do that, that record should be basically in like a kind of a pending state. And from there, the competition service will drop the code and all of the metadata needed to process that submission onto somewhere else. And that somewhere else will— we can use Kafka for, for this. I'm trying to think if like Redis Streams would be good too, but I think Kafka is fine for now.

Verdant Goblin: Mm-hmm.

Magnetic Hare: Basically, we're going to have messages here for, you know, user slash submission ID. Yeah, basically we just need this submission ID. We don't have to denormalize a bunch of stuff. We just need this submission ID to be processed essentially.

Verdant Goblin: Yeah. So do you also think that the code, the submitted code also will be stored in the Postgres or? It's more of unstructured data, right? Where do you want to store?

Magnetic Hare: Yeah, I think there's a trade-off here between denormalizing that data by also storing it in Kafka versus just putting a pointer in Kafka. I think it's fine to just have the pointer be there as a message that needs to be processed.

Verdant Goblin: Right. Now I understand the queuing part, right, where you just send the pointer. But where does that actual code persist? Is it in the Postgres or? Yeah.

Magnetic Hare: Maybe I'll talk about what the submissions looks like. Submission ID, raw code string for that. Basically the code for that submission. We'll persist that first in Postgres. And then it'll have a status, essentially, status, you know, pending or processed. And yeah, and each submission is basically tied to a problem ID, FK, and submission ID is your primary key. And for this particular user ID as well, which is a foreign key on the users table.

Verdant Goblin: Okay.

Magnetic Hare: Yeah, so, yeah, so basically, yeah, so this is where the code is. We're gonna persist the code. I think the code itself, like, maybe you can help me if there is, you know, I'm missing something, but are the submissions for these projects, you know, typical [REDACTED] problems where it's just, you know, maybe like 300 lines of code, or do you see these as like projects that need to be persisted somewhere else first?

Verdant Goblin: Uh, well, I assume that the code, uh, length could be like 5 KB on average.

Magnetic Hare: Okay, so yeah, yes, 5 KB per submission. And I'm gonna definitely come back to the, you know, how's— how does this database actually work? Are we gonna shard this? You know, does it make sense? But, um, yeah, let me just parking lot this for now until we can get the full, um, system working. So On the submission part, they've persisted their code first. They're going to put a submission ID onto a queue, and then we're going to have the different workers, code workers, code evaluator service. That's going to be Reading to— that's basically going to be reading the submissions and then calling out to the database to basically get the submission code and test cases.

Verdant Goblin: Mm-hmm.

Magnetic Hare: Yeah, so basically it gets the test cases, it runs in a safe environment. I don't personally work in like a virtual machine space. I'm not sure what the right like solution is. Do we create Docker spaces or something for where this code actually runs to provide some security between the code running in basically a safe environment but not directly on the machines? But once that code runs in some isolated environments, then also we're going to write out the evaluation results directly to the database. And I think this is an interesting conversation as well.

Verdant Goblin: Mm-hmm.

Magnetic Hare: When you write out the evaluation results directly to the database, so this is going to be Yeah, in basically a results table.

Verdant Goblin: Mm-hmm.

Magnetic Hare: It's— I mean, this is where lots of interesting things can happen. You, you know, what if the code— what if the service dies as it's evaluating the code before it's actually written out the evaluation results? One thing that we want to make sure is that when you write the results, you update the status in the submissions row to processed. And you atomically write the results at the same time. And the reason why we'd wanna do this is because, at least with Kafka, this is— if this works in a perfect world, you wouldn't need to worry about any of these things, but Kafka is going to keep delivering until that message is ACKed, and so it's at least-once delivery, and so what we want is to make sure that any evaluator service that picks up a message basically doesn't try to rewrite or fail ultimately to process the submission. And by making sure that we check a couple different things— so before we even do this, we check whether the status is processed. If it is, that means, you know, we, we don't need to do this anymore. or if it dies, Kafka will try to resubmit that job, or the evaluators will rerun that job, and then we can make sure that the submissions and results table get updated atomically at the same time. Does that make sense?

Verdant Goblin: Yeah, that's fine. I'm more interested in the code evaluator service. I know that we are at very high level. What do you think, like, because people can submit code in different languages, right? How does it execute in that environment or the evaluator service?

Magnetic Hare: Yeah, I'm not entirely sure from an infra perspective what the right way of doing this. I'm assuming that each evaluator service is fungible when it comes to the different runtime environments that these run in, but if that's not possible, then we might want to tag in— even in Kafka, like what type of code submission this is. Like, is this a Java submission? So we need a machine that has JVM. installed on it, or is this, you know, something else? You know, is that something that, you know, I'm not familiar with this like dev infra environment, but I think that my assumption in making this is that we can just install any sort of, you know, Docker. Like basically every machine comes with every code environment pre-installed. I don't know if that's a reasonable assumption.

Verdant Goblin: Right, right. I think that's a reasonable assumption. The next thing is once it evaluates the code, it has to run the test cases. What do you think about the test cases? Do they have separate test cases for each language or how that can be addressed?

Magnetic Hare: The test cases, I'm trying to think of what the trade-offs might be of having a different Every— from my understanding, at least the languages that I've used, like you might use Jest, for example, for JavaScript test cases. I'm not familiar of a way that can have completely agnostic code test results. There are some agnostic things, things like I know, for example, in [REDACTED] we test things like memory usage and space complexity. And so there might be some way to measure metrics that are universal towards every language that we have, but I do think we would need to persist test cases that are unique for every language when we first create the competition.

Verdant Goblin: All right, uh, let's, uh, quickly move on. Uh, yes, uh, let's talk about the leaderboard, how you want to organize the data.

Magnetic Hare: Yeah, yeah, great point. So, um, I'm gonna skip through the like competition, um, ending as, as well, but basically the leaderboard, um, uh, we can talk about that. That's where I was getting a little bit caught up with like why we need to use WebSockets, but we'll have a separate service for this for the leaderboard. And I am hoping that if we set up our table correctly, this should just be a very simple Postgres query against our database. And the reason why I think this is possible is because If we can structure our database for the leaderboard such that we have competition ID as a— every competition ID is unique for this particular leader table. So primary key here.

Verdant Goblin: Mm-hmm.

Magnetic Hare: I'm trying to think through, like, as the results are being written, I think we actually need a— trying to think, like, so the user is submitting the submissions, the results are being updated. So, in the results, let me think through how the results are actually being calculated.

Verdant Goblin: Okay.

Magnetic Hare: Because the competition ID. We have a submission ID, and then we have user, and then we have test results. I'm assuming test result will have the the number itself. So I'm gonna do something really simple at first to solve this. So This is Vensper.

Verdant Goblin: Yeah. Assume that the code evaluator service, once it submits a code and executes the test cases, it updates some number to the user.

Magnetic Hare: Right.

Verdant Goblin: That's all. With that, how do you build this leaderboard?

Magnetic Hare: Yeah. A very naive way of doing this, even before having the leaderboard service, would be we could actually do a live query even for a given competition. Sum up all of the row group, every test result by user, and then sort those by the test, like the aggregate test results. And then once you sort that, that is your ordering for the particular users that exist. That would be very slow, however, because as users create more and more results, you would end up getting progressively slower. One way that we could actually speed this up, and I'm gonna do something kind of just off the top of my head, is basically caching this result.

Verdant Goblin: Mm-hmm.

Magnetic Hare: And so, one way that we could do this would be you have a leaderboard cache. Uh, which basically is a sorted set of, uh, user ID. So the key would be leaderboard, uh, you know, leader ID, a sorted set of user and score. And what we could have is basically a periodic job that's doing exactly what I basically mentioned, and then basically filling out the cache with that data over time. And it's basically just overwriting that. And this is where we can satisfy the— Basically the latency aspect. So, as leaderboard cache entries are written, even these could be versioned essentially. So, we're not sending new stale versions, but you could have a service basically that generates the leaderboard. Basically like a cache loader service. Periodically polling the database and writing to cache. So I think this is this is one approach. This would work. There there's some downsides. Obviously, the leaderboard doesn't really update in real time. There's a trade-off here between the how often the cache loader is running and slamming our database versus you know if you want completely consistent results, you have to compute this. in real time. And I think there's actually— there has to be, I think, a better way of doing this, in my opinion, than the results. I believe there should be a way to materialize the view of results as they're being written.

Verdant Goblin: Mm-hmm.

Magnetic Hare: Which would actually, I think, help us at that point update this cache faster because then you might not need a cache loader at all at that point. Instead, an optimization there could actually be once you've materialized that view of the leaderboard, you can actually just use CDC at that point to basically CDC directly to this leaderboard cache, which would eliminate this cache loader being a point of failure for our system. So, That's definitely something that we could do, but this solution will work, you know, assuming we're running this every 30 seconds or something like that while the competition's going.

Verdant Goblin: Okay. So how do you scale the system? For example, the competition service writing to a database, right? What happens if that connectivity is lost? How do you handle those failure points between competition service to database?

Magnetic Hare: So do you mean in terms of the, like, as the submissions are happening essentially, or— I just want to clarify, you mean like as the submissions are happening, or do you mean in the context of a different use case?

Verdant Goblin: As the submissions are happening, the competition service— my understanding based on our discussion, the competition service first writes the submission to database and then, and then get the submission ID and post it to Kafka.

Magnetic Hare: Yeah.

Verdant Goblin: Right. What happens if the database is available but the network partitioning has happened between competition service to database? How do you handle that?

Magnetic Hare: Yeah, let's talk about the different, um, cases that can happen. So user submits to, uh, competition service. Um, the first thing that we need to do is definitely to write the submission, uh, into the database. So db.write to submission, uh, uh, table. If this fails, uh, We— so 2A failure, the user needs to retry at that point. So we'll surface an error to the user that the submission failed, retry. Hopefully this doesn't happen and we'll deploy this competition as a scaled stateless service to account for the traffic that we have.

Verdant Goblin: Mm-hmm.

Magnetic Hare: But if we fail, we— the user basically needs to retry. And then we move on to the second piece of persist to Kafka. Even it's possible the failure could come in between where the database write happens, but, you know, we are unable to persist to Kafka. That's where actually maybe I should put a 2-0. I still call this B.

Verdant Goblin: Yeah, good, good. Got it.

Magnetic Hare: Yeah, so basically, um, we need to check if that submission for that user, um, for that particular problem, um, exists, uh, already. If it does, then we need to pull that specific submission back. Um, for, for this, we actually, uh, would need a concept of, uh, client idempotency keys because the user can basically submit for the same problem multiple times, and we don't want to create multiple entries in the database for that. And so the submission ID that we're using here, we can assume this is actually the client submission ID for that particular problem ID, which I'm assuming is specific to that particular problem set. So we definitely don't want to write multiple times because users are going to retry multiple times, and that competition also doesn't— should not be writing multiple times. Anyway. Okay. Um, yes. All right. Sorry.

Verdant Goblin: Yeah, I think, I think we are almost, uh, time. We can use the rest of the time to exchange feedback.

Magnetic Hare: Yeah, that's fine. Yeah, yeah. Thanks.

Verdant Goblin: All right. Um, so first of all, uh, what do you think about this exercise? Uh, what, what do you think what you did right, what you could have done better?

Magnetic Hare: Yeah, I think I think I was able to get through the requirements, but I think I spent a really long time on like the high-level API and I didn't actually use the, like, the write throughput numbers and scalability numbers very much at all.

Verdant Goblin: Yeah.

Magnetic Hare: I kind of alluded to how they would be used and talked through like some of the low-level details of why it's important to, you know, atomically write some of these results. But I don't think we got to like as much of the deep dive sections as I kind of wish I could have gotten to. But yeah, yeah, but I think, yeah, yeah, that's basically it. Yeah. I'm curious to hear your thoughts.

Verdant Goblin: Yeah, I think I feel that most of the parts of the interview did well, right? Only very few things probably that can, uh, that is very much needed, especially when you're going for senior-level positions, right? The major crux of this problem is all about security isolation, right? The sandboxing. How do we ensure that the code is running without impacting the rest of the system, right? So this is where network-level isolation, sandboxing, and also probably the different options. Probably where what you could have discussed is, you know, normally the junior candidates will say that we will run as part of the AP server, which is not a great idea, right? The next better answer would be using, using a virtual machine, right?

Magnetic Hare: Mm-hmm.

Verdant Goblin: And then the next best answer would be to use a containerization. Use containers that will have different runtimes. It has Python runtime, Java runtime, everything. You need to submit the code, it will take care of running that code. One more level you can go is using the serverless function. That is something which you can use like AWS Fargate. Those are some of the things also you can use it. I think you touched upon very high level, but probably going a little bit deep is very much needed for senior level candidates.

Magnetic Hare: That makes sense.

Verdant Goblin: That is one area. The other area is test cases, what we probed about the test cases. Normally, they don't have a test case for each language. Normally, they allow one standard format. What they do is based on the language, they serialize the object, whatever that object input, to corresponding the standard format, and that will run. Otherwise, it will not— the system will not scale, right? Because every language, that is case. That is one area you may want to go deep. And then the leaderboard is something— the sorted set is the great data structures for this. Probably, probably going into more detail about how internally it works is based on the HashMap and the SkipList, right? That's how internally it can search the data in log n time, right? More efficient. And also the WebSocket is one of the things, you know, WebSocket is also correct answer, but the best answer I would say for this particular interview is the server-sent event. Not even server-sent event, it's polling. Yes. It's okay. As soon as the user logs in, we can just go and get that leaderboard first, and every 5 seconds we can poll and get that latest leaderboard from sorted set, right? That will work better because WebSocket is overkill for this particular thing because we are not— it's not like a real-time chat where you need to see the new message. Yeah, very frequently.

Magnetic Hare: That makes sense, right?

Verdant Goblin: Yeah, so that, that is another thing. Um, the other one, uh, I think we talked about the cursor pagination, the API. Um, I, uh, I think pretty much you covered, uh, everything. Uh, uh, probably the— yeah, I think everything is covered. I don't have much comment over here. Uh, and then the non-functional requirements is something which I have, uh, some feedback. So I think the— always start with the CAP theorem. I would recommend people to start with the CAP theorem. You know, you cannot build CA systems, you can build only CP or AP systems, right? That I think you discussed in the first 2 points, right? The leaderboard can be eventually consistent, but the availability is also equally important, right?

Magnetic Hare: Correct.

Verdant Goblin: Because for the competitions, the system should be available for the users to play and That is something which could have come as part of discussing non-functional requirements.

Magnetic Hare: Right.

Verdant Goblin: And anything you discuss in non-functional requirements, let's support with proper number. For example, availability means it's a 99.99, whatever, 5 nines, 4 nines, right? And we talked about the latency, the QPS calculations could have been better.

Magnetic Hare: Right.

Verdant Goblin: I think we add this number, but what this number mean to you for designing the system, right? So peak, yes, 50K submissions, but how it is going to impact your design decisions, that is, that didn't come out very well, right? At least if you are converting that to your QPS, okay, this is a QPS, you know, based on those high-level numbers. If you can calculate the QPS and then this is the QPS I'm targeting this system for, right? And that would have added more weightage there. Yeah, otherwise the nonfunctional requirements could have covered more on fault tolerance, resilient systems, right? These are unreliable, basically reliability, right? How the systems can be fault tolerant, reliable.

Magnetic Hare: Mm-hmm.

Verdant Goblin: I think mentioning those, uh, things also very important for non-functional requirements. Um, otherwise, I think the time-wise, uh, for— I, uh, I think we were able to complete the first 30 minutes including the high-level APS, which is great, right? Um, that very, very, uh, very rare I used to see candidates completing all these things. And you are able to follow the structure, proper structure, and complete these core entities, high-level APIs, including the functional, non-functional requirements within 30 minutes is good. But I would, I would recommend if you can squeeze there by 5 minutes, right, 25 minutes, if you can take, that would be nice. Because I always recommend first 12 minutes to functional and non-functional requirements, probably another 13 minutes to cover both core entities and high-level APIs. That will complete those sessions by 25 minutes so you have ample time for, um, the architecture and, uh, going into deep dive section, because most of the time will be spent in the deep dive session, right? I think we need to ensure, uh, we have enough time for that. Um, that's, uh, that's pretty much I have. Uh, otherwise, your functional requirements, you are very detailed. I think you are able to ask the right clarifying questions to finalize the requirements. And you, you are methodical, followed the structure without giving that direction, which is great. Adding some numbers to the non-functional requirements, and few non-functional requirements were missed. Probably you may want to focus on that. And then the architecture diagram, going into details about isolation, which is a crux of the system design, and, and the the leaderboard, right? What is the pros and cons of using WebSocket versus the polling? But sometimes polling is the best solution.

Magnetic Hare: Yeah, that makes a lot of sense. Yeah, this is really good feedback.

Verdant Goblin: Yeah, yeah, yeah, that's pretty much it.

Magnetic Hare: Yeah, thank you so much. Yeah, I'm looking forward to the write-up. I was trying to take some notes while you were sharing too. So yeah, anything else for me?

Verdant Goblin: Um, that's it for now. Good luck.

Magnetic Hare: Thank you.

Verdant Goblin: For you. Bye. Have a nice day. Bye.

\ \ Microsoft Interviewer\ \ Unique ID generation\ \ Invincible Cloud,aMicrosoftengineer, interviewed Golden Possum\ \ Watch interview](/content/mocks/microsoft-system-design-unique-id-generation/index.html)

\ \ Google Interviewer\ \ Order statistic of an unsorted array\ \ Intergalactic Avenger,aGoogleengineer, interviewed Supersonic Taco in Java\ \ Watch interview](/content/mocks/google-java-order-statistic-of-an-unsorted-array/index.html)

\ \ Google Interviewer\ \ Most frequent integer and pairs of integers sum\ \ Paisley Wallaby,aGoogleengineer, interviewed Propitious Bear in Java\ \ Watch interview](/content/mocks/google-java-most-frequent-integer-and-pairs-of-integers-sum/index.html)

\ \ Google Interviewer\ \ Triplet Array\ \ Rocket Wind,aGoogleengineer, interviewed Whirlwind Alligator in C#\ \ Watch interview](/content/mocks/triplet-array/index.html)

See more interviews

We know exactly what to do and say to get the company, title, and salary you want.

Interview prep and job hunting are chaos and pain. We can help. Really.

Get started for free