I would like to share my experience passing 45 min product design interview(NOT a system design interview) in a tech company. Everything described below is my experience, it is NOT a benchmark answer and does not claim to be one.
You need to design an API to implement a news feed like in the following picture.
As usual, the task itself is very high level and unclear, and our first goal is to ask as many important questions as possible to understand the context of the task. Let's ask the first portion of questions:
There are definitely much more questions that we can ask, but we should remember that time is limited, and we only need to get the most critical information to make decisions.
Based on the answers above, we decided to start designing the required API. Let's imagine that we have to use REST API. So we need at least one endpoint to get a list of recent posts:
GET /posts
{
"count": 2
}
{
"posts": [
{
"picture_url": "",
"text": "",
"comments_ammount": 42,
"likes_amount": 146
},
{
"picture_url": "",
"text": "",
"comments_ammount": 22,
"likes_amount": 246
}
]
}
This endpoint allows us to render the exact layout we need, but it has multiple problems:
This is not an exhaustive list of issues, but in my opinion, they are the most critical, so let's try to fix them.
So how can we solve issue #1? I can see two ways here:
It's hard to even call it a trade-off, as the choice is obvious. The best option is to use pagination, it allows us to get posts proportionally.
{
"offset": 0,
"limit": 2
}
{
"offset": 0,
"limit": 2,
"posts": [
{
"picture_url": "",
"text": "",
"comments_ammount": 42,
"likes_amount": 146
},
{
"picture_url": "",
"text": "",
"comments_ammount": 22,
"likes_amount": 246
}
]
}
Now we can get posts batch by batch, which helps us to avoid rendering the same post twice.
To solve problem #2 we need to add more fields to be able to sort our posts. We have at least 2 ways here:
how
to sort and what
to sort. For example, sort in ascending
order by comments_ammount
field. It makes our solution flexible but increases complexity.how
to sort. For example, sort in ascending
order – get the oldest posts first.I would choose a more simple option here for now, anyway it can be improved later.
{
"offset": 0,
"limit": 2,
"sort": "ASCENDING"
}
{
"offset": 0,
"limit": 2,
"posts": [
{
"picture_url": "",
"text": "",
"comments_ammount": 42,
"likes_amount": 146,
"date": "2022-09-08T09:45:55Z"
},
{
"picture_url": "",
"text": "",
"comments_ammount": 22,
"likes_amount": 246,
"date": "2022-09-10T09:45:55Z"
}
]
}
Let's move on to problem #3 – how to see comments and likes. Again, we have multiple ways of solving this:
post_id
. This approach will have better performance for feed loading, as we will get less data. But it makes users wait when they want to see comments.The hybrid approach looks the most appropriate here, and it doesn't require a lot of effort, so let's use it.
GET /posts
{
"offset": 0,
"limit": 2,
"sort": "ASCENDING"
}
{
"offset": 0,
"limit": 2,
"posts": [
{
"id": 12345,
"picture_url": "",
"text": "",
"comments_ammount": 42,
"likes_amount": 146,
"date": "2022-09-08T09:45:55Z",
"top_comments": [
{
"id": 1,
"author_id": 2,
"text": ""
},
{
"id": 2,
"author_id": 4,
"text": ""
}
],
"top_likes": [
{
"id": 1,
"author_id": 5
},
{
"id": 2,
"author_id": 8
}
]
},
{
"id": 12346,
"picture_url": "",
"text": "",
"comments_ammount": 22,
"likes_amount": 246,
"date": "2022-09-10T09:45:55Z",
"top_comments": [
{
"id": 4,
"author_id": 10,
"text": ""
},
{
"id": 5,
"author_id": 11,
"text": ""
}
],
"top_likes": [
{
"id": 6,
"author_id": 12
},
{
"id": 7,
"author_id": 16
}
]
}
]
}
GET /posts/{id}/comments
{
"offset": 0,
"limit": 4,
"sort": "ASCENDING"
}
{
"offset": 0,
"limit": 4,
"comments": [
{
"id": 4,
"author_id": 10,
"text": ""
},
{
"id": 5,
"author_id": 11,
"text": ""
},
{
"id": 6,
"author_id": 10,
"text": ""
},
{
"id": 7,
"author_id": 11,
"text": ""
}
]
}
The endpoint for likes looks the same, so we omit it here.
It is likely that by this point the 45-minute interview will have come to an end, and it is worth finalizing the decision. We have certainly prepared a working API, but there are still a lot of problems, and it is worth going over them quickly.
offset
will be shifted and on the next request, we will receive a duplicated post.preview
– small but low-quality and fullsize
– big with good quality?Note that there are several options for solving an issue (trade-offs), your job as a candidate is to show that you see them and try to justify your choice of the best option. And don't worry if after the interview you understand, that you miss something. The goal of the interview is to see the way, you're thinking, not to prepare the real design of the product.
Also Published here