Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Round 4: [System Design]

 We have 500TB key - value database. Need design system that will value by key. You
can compute how many machines you need. Database sharding , backups . What will
be if one of machines will die?

 Make a sharding of the database by key. Let's say you have 500 machines with 1 TB for each.
Make sure to have a load balancer to point to a right shard based on the key.
What If 500TB becomes 1000TB, USE the consistent
hashing http://deepakdhakal.com/post/2019/08/10/Consistent-Hashing-Save-Your-
Weekends.aspx
 for each machine, make sure to add another 2 more backups and do an eventual consistency
policy on update.

Design a system like Google photos homepage that can show recent photos of a user along
with the shared photos.
Interest areas -

 data store & entities involved


 system apis
 how recent photos can be shown (order/ranking algorithm) with scrolling feature in
mind
Google | Onsite | Get all logs between times
Design an API that supports the following function calls:

public class ServiceManager {

public void addService(String service) {

// todo

public void addServiceCall(String service, long time, String payload) {

// todo

public List<String> getAllServiceCallsBetweenTimes(long time1, long time2) {

// todo

Example:

ServiceManager m = new ServiceManager();

m.addService("A");

m.addService("B");

m.addServiceCall("A", 1, "abc");

m.addServiceCall("A", 5, "abec");

m.addServiceCall("B", 3, "ac");

m.getAllServiceCallsBetweenTimes(1, 4); // ["abc", "ac"]

Follow-up:
What if you also had to filter by service?
Hi,
Well, I think this question is very straightforward. The thing is... store the services, sorting by time.
There are a plenty of data structures that can make it for you.

 Solution 1
Worst solution. You create a Linked list (or an array) and when a
call getAllServiceCallsBetweenTimes() is triggered you walk through this list and adds
every task that satisfies the condition into a temporary list and return it.
It is O(n) in time, since you have to walk through the entire list every time this call is triggered
and O(n) (worst case) in space, cause you have to create another structure to accommodate
the services to return.
 Solution 2
Store in a BST. Every time a new element is added to the tree you add it (one problem here is
to keep the tree a BST). Once a call is made you just have to find all services in a specific sub
tree range. Since a search in a BST is O(log n) you would have an exceptional performance
improvement.
 Solution 3
Sometime we forget about a very nice data structure... heap. The heap keeps the smaller (or
greater) element in the top. All you have to do is.. keep the services in this heap and go
removing and once you find the first element that is in the lower range you keep tracking
and finish when found the first element greater than the higher boundary. It is also a O(log n)
structure and can be very helpful in this situation.

If you want to add filter by service, after getting all elements from the range, look for the service you
want. Or, just discard elements that do not match the service criteria when building the returning list.

Well, that's what I would do.

HTH

Artur

Heap is a good idea! I was thinking maybe balanced BST, which may have better average case
performance on range queries. Worst case complexity for both would be the same though

System design Onsite at FAANG company


Design a live broadcasting system like Twitch with the following functionalities:

 Live video stream


 Display total online users in the room (prompted me to talk about multiple different
implementation & tradeoff)
 Send gifts to room owner with a message broadcasted to all users in the room ( assume
multiple gifts & msg can appear on the same screen at once, how to make sure they
are in order)
 Room chat function
 Playback video function should display gifts, screen messages, & chat to be shown
again. (after broadcast is offline)

Also asked about tradeoff on ajax polling, long polling, & web socket for each of the
functionality and ask me which to use for each.

Please discuss!

Ans:
Here is what I'm thinking:
After clarifying the feature requirements, ask about the consistency/availability/latency requirements.
Then talk about the high level design, drop some boxes, walk through the features one by one. Then
describe how you solve the issue, what's the inner database design, API design, communication
protocol inside it. Make sure you keep on talking and gain some guidance from the interviewer,
understand which part he wants the most.
Sometimes I found it doesn't mean you need to go over each component of the system, interviewer
may only want to focus on some small part. This means the system design process may vary a bit
totally depending on the interviewer.

*Live video stream


live stream usually use UDP protocol, which enables client to get stream contents both from the
server and from the peers. Client could ask Server to get the peers list, then pick the top few peers
and setup connection, when client need to get a stream content, he asks the peers as well as the
server to get it, then client side will put the video pieces into correct order.

*Display total online users in the room


I would firstly talk about the database design, chatroom info table contains metadata info, like what
is this chat room about, which live stream it ties to. Then there is another table called chat room
users table, which contains the chat room id, and the list of the user ids. So we can get the user id list
based on chat room info.
Then the next part is to figure out who is online or offline, for this part, we can talk about setting up
the web socket connection from the client to the server, and in server side we have a hashMap which
stores the user id to connection object map, also we can store a reverse map, given the connection,
we can find out the user id, in this way if the connection is gone, we can update the user
online/offline status accordingly.

*Send gifts to room owner with a message broadcasted to all users in the room
The action here contains 1. send gifts, clarify with the interviewer whether you need to describe this
part. 2. After send gifts, the system will generate a message broad cast to all users in the root, so the
system itself could generate the send gift message, then figure out who are the users in the chat
room, go to the user id <-> connection object hash map table figure out which connection is used
to connect to the customer, then send the message through the connection.
You may want to talk about if the user is offline, then you need to queue/store the message in some
place, then send later. Either after user come online or send notification.

*Room chat function


You can talk about the table design, there is a table called roomchat table, and it contains the room
chat id, also the room id, each chat message contains a message id, and each message contains the
user id and the chat content. More like a ER diagram talk here.

*Playback video function should display gifts, screen messages, & chat to be shown again
Firstly you want to talk about video has the time line, then gifts, screen messages and chats are all
having the time line, you may want to talk about the algorithm of this implementation, like you have
a few components hooked to the video timeline, then those components will add
gifts/message/chats into the chatroom/video when the video timeline hits.

Hey guys,

I was interviewed at Twitch earlier This was the question that was asked for sys design.

Given

 There is already defined Twitch video stream and chat box with a viewer count
 There is a User Service that requires User_id and provides User's Age and Gender.

The interviewer is a PM and wants to check if this feature is possible.


Question
Need to add Analytics dashboard of all viewers based on Gender, Agde distribuution graphs
below the video stream.

Approach

 I had a WebSocket approach where the user devices provide heartbeat checks to server
and Server produces data analytics on active users.

Google | Phone Screen | Design a webpage that can show 10M+ users status
Design a webpage that can show 10M+ users status including name, photo, badge, points.

(1) The points are updated in real-time


(2) Write code that retrieves info from API and update them to the DOM elements

1. depending on how you design the system and layout. It's an open-ended and progressive
question that you'll define along the way. I gave an example of infinite scrolling / sliding
window to represent how to update / store users.
2. a fetch is the first step, it follows up with (1) implementing the model / view (as in MVC)
logics for updating the page (2) DOM API manipulations with scroll events and pass to (1)

Infinite scrolling would be one way.

1. Another optimization would be calling backgroundFetch API on chrome and fetch


asyncronously (for the next page).
2. You can do greedy fetching of the data based on the connection API from chrome. I.e for
mobile browsers on 2G, you can be aggressive and fetch more data in the background
instead if you have a good connection, only fetch when it's needed. This can be a config like
at what % scroll of your page, do you want to call FetchData() function that fetches your
API. (For scrolling you can use, scrollTop and scrollHeight to identify when you'd like to fetch)
3. For MVC part, you could use Vanilla Javascript when you're doing stuff on whiteboard
(keeping it tech agnostic).
4. You could use some Service Workers or LocalStoage() or some sort of catching mechanism to
cache the assets on the front-end. That way you aren't duplicating the calls.
5. For the realtime update part, you can use Socket.io or if it's realtime latency sensitive data
then we could use WebRTC for realtime communication.

You might also like