<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dibas Behera on Medium]]></title>
        <description><![CDATA[Stories by Dibas Behera on Medium]]></description>
        <link>https://medium.com/@dibasbehera?source=rss-97dc6053d9e7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Fln5HYtO9K00QzcwIGXSrw.png</url>
            <title>Stories by Dibas Behera on Medium</title>
            <link>https://medium.com/@dibasbehera?source=rss-97dc6053d9e7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:56:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dibasbehera/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Machine Learning System Design Interview — Event Recommendation System]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-event-recommendation-system-1d7f827b2ea1?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d7f827b2ea1</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Sat, 23 May 2026 20:06:00 GMT</pubDate>
            <atom:updated>2026-05-23T20:06:00.541Z</atom:updated>
            <content:encoded><![CDATA[<p>In this chapter, we design an event recommendation system similar to Eventbrite’s. Eventbrite is a popular event management and ticketing marketplace which allows users to create, browse, and register events. A recommendation system personalizes the experience and displays events relevant to users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*Ib1TJo0OKxek-akXU-tILw.png" /></figure><h3>Clarifying Requirements</h3><p>Here is a typical interaction between a candidate and an interviewer.</p><p><strong>Candidate:</strong> What is the business objective? Can I assume the main business objective is to increase ticket sales?<br><strong>Interviewer:</strong> Yes, that sounds good.</p><p><strong>Candidate:</strong> Besides attending an event, can users book hotels or restaurants on the platform?</p><p><strong>Interviewer</strong>: For simplicity, let’s assume only events are supported.</p><p><strong>Candidate:</strong> An event is considered an ephemeral one-time occurrence item that only happens once, and then expires. Is this assumption correct?<br><strong>Interviewer:</strong> That’s an excellent observation.</p><p><strong>Candidate:</strong> What event attributes are available? Can I assume we have access to the textual description of the event, price range, location, date and time, etc.?<br><strong>Interviewer:</strong> Sure, those are fair assumptions.</p><p><strong>Candidate:</strong> Do we have any annotated data?<br><strong>Interviewer:</strong> We don’t have a hand-labeled dataset. You can use event and user interaction data to construct the training dataset.</p><p><strong>Candidate:</strong> Do we have access to the user’s current location?<br><strong>Interviewer:</strong> Yes. Since this problem focuses on a location-based recommendation system, let’s assume users agree to share their location data.</p><p><strong>Candidate:</strong> Can users become friends on the platform? Friendship information is valuable for building a personalized event recommendation system.<br><strong>Interviewer:</strong> Good question. Yes, let’s assume users can form friendships on our platform. A friendship is bidirectional, meaning if A is a friend of B, then B is also a friend of A.</p><p><strong>Candidate:</strong> Can users invite others to events?<br><strong>Interviewer:</strong> Yes.</p><p><strong>Candidate:</strong> Can a user RSVP to an event?<br><strong>Interviewer:</strong> For simplicity, let’s assume only a registration option is available for an event.</p><p><strong>Candidate:</strong> Are the events free or paid?<br><strong>Interviewer:</strong> We need to support both.</p><p><strong>Candidate:</strong> How many users and events are available?<br><strong>Interviewer:</strong> We host around 1 million total events every month.</p><p><strong>Candidate:</strong> How many daily active users visit the website/app?<br><strong>Interviewer:</strong> Assume we have one million unique users per day.</p><p><strong>Candidate:</strong> Since we are building a location-based event recommendation system, it’s important to calculate the distance and travel time between two locations efficiently. Can we assume external APIs such as Google Maps API or other map services can be used to obtain such data?<br><strong>Interviewer:</strong> Good point. Assume we can use third-party services to obtain location data.</p><p>Let’s summarize the problem statement. We are asked to design an event recommendation system, which displays a personalized list of events to users. When an event is finished, users can no longer register for it. In addition to registering for events, users can invite others to events and form friendships. The training data should be constructed online from user interactions. The primary goal of this system is to increase total ticket sales.</p><h3>Frame the Problem as an ML Task</h3><h4>Defining the ML objective</h4><p>Based on the requirements, the business objective is to increase ticket sales. One way to translate this into a well-defined ML objective is to maximize the number of event registrations.</p><h4>Specifying the system’s input and output</h4><p>The input to the system is a user, and the output is the top k events ranked by relevance to the user.</p><h4>Choosing the right ML category</h4><p>There are different ways to solve a recommendation problem:</p><ul><li>Simple rules, such as recommending popular events</li><li>Embedding-based models which rely on content-based or collaborative filtering</li><li>Reformulating it into a ranking problem</li></ul><p>Rule-based methods are good starting points to form a baseline. However, ML-based approaches usually lead to better outcomes. In this chapter, we reformulate the task into a ranking problem and use Learning to Rank (LTR) to solve it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*p28N7eMuVLgmZw3F6ShKig.png" /></figure><p>LTR is a class of algorithmic techniques that apply supervised machine learning to solve ranking problems. The ranking problem can be formally defined as: “having a query and a list of items, what is the optimal ordering of the items from most relevant to least relevant to the query?” There are generally three LTR approaches: pointwise, pairwise, and listwise. Let’s briefly examine each. Note that a detailed explanation of these approaches is beyond the scope of this book. If you’re interested in learning more about LTR, refer to [1] .</p><p>Pointwise LTR</p><p>In this approach, we go over each item and predict the relevance between the query and the item, using classification or regression methods. Note that the score of one item is predicted independently of other items.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*3aqWSLILI-MT7t7skuzoJA.png" /></figure><p>The final ranking is achieved by sorting the predicted relevance scores.</p><p>Pairwise LTR</p><p>In this approach, the model takes two items and predicts which item is more relevant to the query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*FZOQ-ED7SFz8QXYTylMwJQ.png" /></figure><p>Some of the most popular pairwise LTR algorithms are RankNet [2], LambdaRank [3], and LambdaMART [4].</p><p>Listwise LTR</p><p>Listwise approaches predict the optimal ordering of an entire list of items, given the query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/486/1*cdU92EG767qgi-bPycis7g.png" /></figure><p>Some popular listwise LTR algorithms are SoftRank [5], ListNet [6], and AdaRank [7].</p><p>In general, pairwise and listwise approaches produce more accurate results, but they are more difficult to implement and train. For simplicity, we use the pointwise approach for this problem. In particular, we employ a binary classification model which takes a single event at a time and predicts the probability that the user will register for it. This approach is shown in Figure 7.6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*-FanHm-Lv9O0g22UzIrveg.png" /></figure><h3>Data Preparation</h3><h4>Data engineering</h4><p>To engineer good features, we need first to understand the raw data available in the system. Since an event management platform is mainly centered around users and events, we assume the following data are available:</p><ul><li>Users</li><li>Events</li><li>Friendship</li><li>Interactions</li></ul><p>Users</p><p>The user data schema is shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*tiJqBQZchi2Mjl9uxRWnaQ.png" /></figure><p>Events</p><p>Table 7.2 shows what the event data might look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*7rPfGNBjIYsAyNpOMJpVeA.png" /></figure><p>Friendship</p><p>In Table 7.3, each row represents a friendship formed between two users, along with the timestamp of when it was formed</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*hfObesRHngPc269wp_2Oww.png" /></figure><p>Interactions</p><p>Table 7.4 stores user interaction data, such as event registrations, invitations, and impressions. In practice, we may store interaction data in different databases, but for simplicity, we include them in a single table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*S2y9OUUlDr5QjRmXBcwPJg.png" /></figure><h4>Feature engineering</h4><p>Event-based recommendations are more challenging than traditional recommendations. An event is fundamentally different from a movie or a book, as there is no consumption after the event ends. Events are typically short-lived, meaning the time is short between event creation and when it finishes. As a result, there are not many historical interactions available for a given event. For this reason, event-based recommendations are intrinsically cold-start and suffer from a constant new-item problem.</p><p>To overcome those issues, we put more effort into feature engineering to create as many meaningful features as possible. Due to space constraints, we will only discuss some of the most important features. In practice, the number of predictive features can be much higher. In this section, we create features related to each of the following categories:</p><ul><li>Location-related features</li><li>Time-related features</li><li>Social-related features</li><li>User-related features</li><li>Event-related features.</li></ul><p>Location-related features</p><p><strong>How accessible is the event’s location?</strong></p><p>The accessibility of an event’s location is an important factor. For example, if an event is high up in hills far from public transportation, the commute may discourage users from attending. Let’s create the following features to capture accessibility:</p><ul><li>Walk score: Walk score is a number between 0 and 100, which measures how walkable an address is, based on the distance to nearby amenities. It is computed by analyzing various factors such as distance to amenities, pedestrian friendliness, population density, etc. We assume walk scores can be obtained from external data sources such as Google Maps, Open Street Map, etc. Table 7.57.5 shows walk scores bucketized into 5 categories.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*Cec-Fq_M9GXG9H1csgei8w.png" /></figure><ul><li>Walk score similarity: The difference between the event’s walk score and the user’s average walk score of previous events registered by the user.</li><li>Transit score, transit score similarity, bike score, bike score similarity.</li></ul><p><strong>Is the event in the same country and city as the user?</strong><br>A very important deciding factor for a user is whether the event is in the same country and city where they are located. The following two features can be created:</p><ul><li>If the user’s country is the same as the event’s country, this feature is 1, otherwise 0</li><li>If the user’s city is the same as the event’s city, this feature is 1, otherwise 0</li></ul><p><strong>Is the user comfortable with the distance?</strong><br>Some users may prefer events that are very close to their location, while others prefer events that are further away. We use the following features to capture this:</p><ul><li>The distance between the user’s location and the event’s location. This value can be obtained from external APIs and bucketized into a few categories. For example:</li><li>0: less than a mile</li><li>1: 1–5 miles</li><li>2: 5–20 miles</li><li>3: 20–50 miles</li><li>4: 50–100 miles</li><li>5: +100 miles</li><li>Distance similarity: Difference between the distance to an event and the average distance (in reality, the median or percentile range can be used) to events previously registered by the user.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*htkFqEiOf_A98_9DByiyCw.png" /></figure><p>Time-related features</p><p><strong>How convenient is the time remaining until an event?</strong><br>Some users may plan events a few days in advance, while others don’t. Let’s create the following features to capture this:</p><ul><li>The remaining time until the event begins. This feature can be bucketized into different categories and one-hot encoded. For example:</li><li>0: less than 1 hour left until the event starts</li><li>1: 1–2 hours</li><li>2: 2–4 hours</li><li>3: 4–6 hours</li><li>4: 6–12 hours</li><li>5: 12–24 hours</li><li>6: 1–3 days</li><li>7: 3–7 days</li><li>8: +7 days</li><li>Remaining time similarity: Difference between “remaining time” and average “remaining time” of events previously registered by the user.</li><li>The estimated travel time from the user’s location to the event’s location. This value will be obtained from external services and bucketized into categories.</li><li>Estimated travel time similarity: The difference between the estimated travel time to the event in question, and the average estimated travel time of events previously registered by the user.</li></ul><p><strong>Are the date and time convenient for the user?</strong><br>Some users may prefer events that occur at weekends, while others prefer weekdays. Some users prefer events in the morning, while others may prefer evening events. To capture a user’s historical preferences for days of the week, we create a user profile. This user profile is a vector of size 7 , and each value counts the number of events the user attended on a particular day. By dividing these values by the total number of attended events, we get the historical rate of event attendance for each day of the week. Figure 7.8 shows the per-day distribution of a user’s previously attended events. As we can see, this user has never attended an event on Monday or Wednesday, so displaying an event that occurs on Wednesday may not be a good recommendation for this user. Per-hour user profiles can be created using a similar approach. Similarly, we add day and hour similarity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*Sq_dfbbxo_2KHraTPDdojA.png" /></figure><p>A summary of time-related features is shown in Figure 7.9</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/424/1*R8D0A4kl4tHYyPCahz3UxA.png" /></figure><h4>Social-related features</h4><p><strong>How many people are attending this event?</strong><br>In general, users are more likely to register for an event if there are a lot of other attendees. Let’s extract the following features to capture this:</p><ul><li>Number of users registered for this event</li><li>The ratio of the total number of registered users to the number of impressions</li><li>Registered user similarity: The difference between the number of registered users for the event in question and previously registered events</li></ul><p><strong>Features related to attendance by friends</strong><br>A user is more likely to register for an event if their friends are attending it. Here are some of the features we can use:</p><ul><li>Number of the user’s friends who registered for this event</li><li>The ratio of the number of registered friends to the total number of friends</li><li>Registered friend similarity: Difference between the number of registered friends for the event in question and previously registered events</li></ul><p><strong>Is the user invited to this event by others?</strong><br>Users are more likely to attend events to which they are invited. Some features that might be helpful are:</p><ul><li>The number of friends who invited this user to the event</li><li>The number of fellow users who invited this person to the event</li></ul><p><strong>Is the event’s host a friend of the user?</strong><br>Users tend to attend events created by their friends. We create a binary feature to reflect this: if the event’s host is the user’s friend, this value is 1, otherwise, 0.</p><p><strong>How often has the user attended previous events created by this host?</strong><br>Some users are interested in following a particular host’s events.</p><p>User-related features</p><p><strong>Age and gender</strong><br>Some events are geared toward specific ages and genders. For example, “Women in Tech” and “Life lessons to excel in your 30 s” are examples of events that may be specific to certain demographic groups. We create two features to capture this:</p><ul><li>User’s gender, encoded with one-hot encoding</li><li>User’s age, bucketized into multiple categories and encoded with one-hot encoding</li></ul><p>Event-related features</p><p><strong>Price of event:</strong><br>The price of an event might affect the user’s decision to register for it. Some features to use are:</p><ul><li>Event’s price, bucketized into a few categories. For example:</li><li>0: Free</li><li>1: $1-$99</li><li>2: $100-$499</li><li>3: $500-$1,999</li><li>4:+$2,000</li><li><strong>Price similarity:</strong> Difference between the price of the event in question and the average price of events previously registered for by the user.</li></ul><p><strong>How similar is this event’s description to previously registered descriptions?</strong><br>This indicates the user’s interests, based on previously registered events. For example, if the word “concert” repeatedly appears in the descriptions of previous events, it may indicate the user is interested in concert events. To capture this, we create a feature that represents the similarity between the event’s description and the descriptions of previously registered events by the user. To compute the similarity, the description is converted into a numerical vector using TF-ID, and similarity is calculated using cosine distance.</p><p>Note, this feature might be noisy as descriptions are manually provided by hosts. We can experiment by training our model with and without this feature, to measure its importance.</p><p>Figure 7.107.10 shows an overview of user features, event features, and social-related features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*TyklLNFxG5uNQFPAbTUXUA.png" /></figure><p>The features listed above are not exhaustive. There are lots of other predictive features that can be created in practice. For example, host-related features such as the host’s popularity, user’s search history, event’s category, auto-generated event tags, etc. At an interview, it’s not necessary to follow this section strictly. You can use it as a starting point and then discuss topics that are more relevant to the interviewer. Here are some potential talking points you might want to elaborate on:</p><ul><li><strong>Batch vs. streaming features:</strong> Batch (static) features refer to features that change less frequently, such as age, gender, and event description. These features can be computed periodically using batch processing and stored in a feature store. In contrast, streaming (aka dynamic) features change quickly. For example, the number of users registered for an event and the remaining time until an event, are dynamic features. The interviewer may want you to dive deeper into this topic and discuss batch vs. online processing in ML. If you’re interested to learn more, refer to [8].</li><li><strong>Feature computation efficiency.</strong> Computing features in real-time is not efficient. You may want to discuss this issue and possible ways to avoid it. For example, instead of computing the distance between the user’s current location and the event’s location as a feature, we can pass both locations to the model as two separate features, and rely on the model to implicitly compute useful information from the two locations. To learn more about how to prepare location data for ML models, refer to [9]</li><li><strong>Using a decay factor</strong> for features that rely on the user’s last X interactions. A decay factor gives more weight to the user’s recent interactions/behaviors.</li><li><strong>Using embedding learning</strong> to convert each event and user into an embedding vector. Those embedding vectors are used as the features representing the events and users.</li><li><strong>Creating features from users’ attributes may create bias.</strong> For example, relying on age or gender to decide if an applicant is a good match for a job, may lead to discrimination. Since we create features from users’ attributes, it’s important to be aware of potential bias issues.</li></ul><h3>Model Development</h3><h4>Model selection</h4><p>Binary classification problems can be solved by various ML methods. Let’s take a look at the following:</p><ul><li>Logistic regression</li><li>Decision tree</li><li>Gradient-boosted decision tree (GBDT)</li><li>Neural network</li></ul><p>Logistic regression (LR)</p><p>LR models the probability of a binary outcome by using a linear combination of one or multiple features. For the details of LR, refer to [10].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/204/1*0sfc5WxVsc4cOpVmlW1TMg.png" /></figure><p>Let’s see the pros and cons of LR.</p><p><strong>Pros:</strong></p><ul><li><strong>Fast inference speed.</strong> Computing a weighted combination of input features is fast.</li><li><strong>Efficient training.</strong> Given the simple architecture, it’s easy to implement, interpret, and train quickly.</li><li>Works well when the data is linearly separable (Figure 7.12).</li><li><strong>Interpretable and easy to understand.</strong> The weights assigned to each feature indicate the importance of different features, which gives us insight into why a decision was made.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*mXHheH6lbF-8Utlua9yPzA.png" /></figure><p><strong>Cons:</strong></p><ul><li><strong>Non-linear problems can’t be solved</strong> with LR, since it uses a linear combination of input features.</li><li><strong>Multicollinearity</strong> occurs when two or more features are highly correlated. One of the known limitations of LR is that it cannot learn the task well when multicollinearity is present in the input features.</li></ul><p>In our system, the number of input features can be very large. Often, these features have complex and non-linear relations with the target variable (binary outcome). This complexity might be hard for LR to learn.</p><p>Decision tree</p><p>Decision trees are another class of learning methods that use a tree-like model of decisions and their possible consequences to make predictions. Figure 7.13 shows a simple decision tree with two features: age and gender. It also shows the corresponding decision boundary. Each leaf node in the decision tree indicates a binary outcome where “+” indicates the given input is classified as positive, and “-” means negative. To learn more about decision trees, refer to [11].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/559/1*0D8G43JzZ5h7c-jEFfp0Tw.png" /></figure><p><strong>Pros:</strong></p><ul><li><strong>Fast training:</strong> Decision trees are quick to train.</li><li><strong>Fast inference:</strong> Decision trees make predictions quickly at inference time.</li><li><strong>Little to no data preparation:</strong> Decision tree models don’t require data normalization or scaling, since the algorithm does not depend on the distribution of the input features.</li><li><strong>Interpretable and easy to understand.</strong> Visualizing the tree provides good insights into why a decision was made and what the important decision factors are.</li></ul><p><strong>Cons:</strong></p><ul><li><strong>Non-optimal decision boundary:</strong> decision tree models produce decision boundaries that are parallel to the axes in the feature space (Figure 7.13). This may not be the optimal way to find a decision boundary for certain data distributions.</li><li><strong>Overfitting:</strong> Decision trees are very sensitive to small variations in data. A small change in input data may lead to different outcomes at serving time. Similarly, a small change in training data can lead to a totally different tree structure. This is a major issue and makes predictions less reliable.</li></ul><p>In practice, naive decision trees are rarely used. The reason is that they are too sensitive to variations of input data. To reduce the sensitivity of decision trees, two techniques are commonly used:</p><ul><li>Bootstrap aggregation (Bagging)</li><li>Boosting</li></ul><p>These two techniques are widely used across the tech industry. It’s essential to understand how they work. Let’s take a closer look.</p><p>Bagging</p><p>Bagging is the ensemble learning method that trains a set of ML models in parallel, on multiple subsets of the training data. In bagging, the predictions of all these trained models are combined to make a final prediction. This significantly reduces the model’s sensitivity to the change in data (variance).</p><p>One example of bagging is the commonly used “random forest” model [12]. Random forest builds multiple decision trees in parallel during training, to reduce the model’s sensitivity. To make a prediction, each decision tree independently predicts the output class (positive or negative) of the given input, and then a voting mechanism is used to combine these predictions to make a final prediction. Figure 7.147.14 shows a random forest with three decision trees.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/572/1*M2_jbf_hMSgzmUBO_sieAA.png" /></figure><p>The bagging technique has the following advantages:</p><ul><li>Reduces the effect of overfitting (high variance).</li><li>Does not significantly increase training time because the decision trees can be trained in parallel.</li><li>Does not add much latency at the inference time because decision trees can process the input in parallel.</li></ul><p>Despite its advantages, bagging is not helpful when the model faces underfitting (high bias). To overcome bagging’s drawbacks, let’s discuss another technique called boosting.</p><p>Boosting</p><p>In ML, boosting involves training several weak classifiers sequentially to reduce prediction errors. The phrase “weak classifier” refers to a simple classifier that performs slightly better than random guesses. In boosting, multiple weak classifiers are converted into a single strong learning model. Figure 7.157.15 shows an example of boosting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*0s_P3Cd4NMUR-X9PjqvdHg.png" /></figure><p><strong>Pros:</strong></p><ul><li><strong>Boosting reduces bias and variance.</strong> Combining weak classifiers leads to a strong model less sensitive to the change in data. To learn more about bias/variance tradeoffs, refer to [13]. Cons:</li><li><strong>Slower training and inference.</strong> Given the classifiers are trained based on the mistakes of the previous classifiers, they work sequentially. This adds to the serving time due to the sequential nature of boosting.</li></ul><p>The boosting method is usually preferred over bagging in practice because bagging is not helpful in cases of bias, whereas boosting reduces the effect of both bias and variance.</p><p>Typical boosting-based decision trees are Adaboost [14], XGBoost [15], and Gradient boost [16]. They are commonly employed to train classification models.</p><p>GBDT</p><p>GBDT is a commonly used tree-based model, utilizing GradientBoost to improve decision trees. Some variants of GBDT, such as XGBoost [15], have demonstrated strong performance in various ML competitions [17]. If you’re interested in learning more about GBDT, refer to [18] [19].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/286/1*9Mcj8RZXoKOYFO_PmbvXng.png" /></figure><p>Here are the pros and cons of the GBDT model.</p><p><strong>Pros:</strong></p><ul><li><strong>Easy data preparation:</strong> Similar to decision trees, it does not require data preparation.</li><li><strong>Reduces variance:</strong> GBDT reduces variance as it uses the boosting technique.</li><li><strong>Reduces bias:</strong> GBDT reduces the prediction error by leveraging several weak classifiers, iteratively improving upon the misclassified data points from the previous classifiers.</li><li>Works well with structured data.</li></ul><p><strong>Cons:</strong></p><ul><li><strong>Lots of hyperparameters to tune</strong>, such as the number of iterations, tree depth, regularization parameters, etc.</li><li>GBDT does not work well on unstructured data such as images, videos, audio, etc.</li><li><strong>Unsuitable for continual learning</strong> from streaming data.</li></ul><p>In our case, since the created features are structured data, GBDT or one of its variants such as XGBoost — is a good choice to experiment with.</p><p>A major drawback of GBDT is that it is unsuitable for continual learning. In an event recommendation system, new data continuously becomes available to the system, such as recent user interactions, registrations, new events, and even new users. In addition, users’ tastes and interests may change over time. It is vital for a good event recommendation system to adapt itself to new data, continuously. Without the possibility of continual learning, it is very costly to retrain GBDT from scratch regularly. Next, we explore neural networks which overcome this limitation.</p><p>Neural network (NN)</p><p>In an event recommendation system, we have many features that might not correlate linearly with the outcome. Learning these complex relationships is difficult. In addition, continual learning is necessary for adapting the model to new data.</p><p>NNs are great at solving those challenges. They are capable of learning complex tasks with non-linear decision boundaries. Additionally, NN models can be fine-tuned on new data very easily, making them ideal for continual learning. If you are unfamiliar with the details of NNs, you are encouraged to read [20].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*QrdOaq3SZoZnoDX2zBJjkw.png" /></figure><p>Let’s see its pros and cons.</p><p><strong>Pros</strong></p><ul><li><strong>Continual learning:</strong> NNs are designed to learn from data and improve themselves continually.</li><li>Works well with unstructured data such as text, image, video, or audio.</li><li><strong>Expressiveness:</strong> NNs have expressive power due to their high number of learning parameters. They can learn very complex tasks and non-linear decision boundaries.</li></ul><p><strong>Cons</strong></p><ul><li><strong>Computationally expensive</strong> to train.</li><li><strong>The quality of input data strongly influences the outcome:</strong> NNs are sensitive to input data. For example, if input features are in very different ranges, the model may converge slowly during the training phase. An important step for NNs is data preparation, such as normalization, log-scaling, one-hot encoding, etc.</li><li><strong>Large training data</strong> is required to train NNs.</li><li><strong>Black-box nature:</strong> NNs are not interpretable, meaning it’s not easy to understand the influence of each feature upon the outcome, as the input features go through multiple layers of non-linear transformations.</li></ul><p>Which model should we select?</p><p>Picking the right model is challenging. We often need to experiment with different models to determine which works best. We can choose the right model based on various factors:</p><ul><li>Complexity of the task</li><li>Data distribution and data type</li><li>Product requirements or constraints, such as training cost, speed, model size, etc In this problem, both GBDTs and NNs are good candidates for experimentation. We start with the GBDT variant, XGBoost, since it is fast to implement and train. The result can be used as an initial baseline.</li></ul><p>Once we have a baseline, we explore the possibility of building a better model with NNs. Neural networks are expected to work well here for the following reasons:</p><ul><li>Massive training data is available in our system. Users continuously interact with the system by registering for events, inviting friends, publishing new events, etc. Given the number of users, this creates a massive amount of data available for training.</li><li>Data may not be linearly separable, and neural networks can learn non-linear data.</li></ul><p>When designing a NN architecture, several hyperparameters must be considered, including the number of hidden layers, neurons in each layer, activation function, etc. These can be determined by employing hyperparameter tuning techniques. NN architectural details are not typically the main focus of ML system design interviews, since there is no systematic way to choose the right architecture.</p><h4>Model training</h4><p>Constructing the dataset</p><p>Building training and evaluation datasets is an essential step in developing a model. For example, let’s look at how we compute features and their labels.</p><p>To construct a single data point, we extract a ⟨⟨ user, event ⟩⟩ pair from the interaction data and compute the input features from the pair. We then label the data point with 1 if the user has registered for the event, and 0 if not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*ldQbUVarzBJJWKxeeSXJzg.png" /></figure><p>One issue we may face after constructing the dataset is class imbalance. The reason is that users may explore tens or hundreds of events before registering for one. Therefore, the number of negative ⟨⟨ user, event ⟩⟩ pairs is significantly higher than positive data points. We can use one of the following techniques to address the class imbalance issue:</p><ul><li>Use focal loss or class-balanced loss to train the classifier</li><li>Undersample the majority class</li></ul><p>Choosing the loss function</p><p>Since the model is a binary classification model, we use a typical classification loss function such as binary cross-entropy to optimize the neural network model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*o8rBN8zZbs1JJg3rhmC_hg.png" /></figure><h3>Evaluation</h3><h4>Offline metrics</h4><p>To evaluate the ranking system, we consider the following options.</p><p><strong>Recall@k or Precision@k.</strong> These metrics are not good fits because they do not consider the ranking quality of the output.</p><p><strong>MRR, nDCG, or mAP.</strong> These three metrics are commonly used to measure ranking quality. But which one is best?</p><p>MRR focuses on the rank of the first relevant item in the list, which is suitable in systems where only one relevant item is expected to be retrieved. However, in an event recommendation system, several recommended events may be relevant to the user. MRR is not a good fit.</p><p>nDCG works well when the relevance score between a user and an item is non-binary. In contrast, mAP works only when the relevance scores are binary. Since events are either relevant (a user registered for it) or irrelevant (a user saw the event but did not register), mAP is a better fit.</p><h4>Online metrics</h4><p>In our case, the business objective is to increase revenue by increasing ticket sales. To measure the impact of the system on revenue, let’s explore the following metrics:</p><ul><li>Click-through rate (CTR)</li><li>Conversion rate</li><li>Bookmark rate</li><li>Revenue lift</li></ul><p><strong>CTR.</strong> A ratio showing how often users who see recommended events go on to click on an event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*oBzZbvcSB5Qh7BMS-9Ar8w.png" /></figure><p>A high CTR shows our system is good at recommending events that users click on. Having more clicks generally means more event registrations.</p><p>However, relying only on CTR as the online metric may be insufficient. Some events are clickbait. Ideally, we would like to measure how relevant recommended events are for the user. This metric is called the conversion rate, which we discuss now.</p><p><strong>Conversion rate.</strong> A ratio showing how often users who see recommended events go on to register for them. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*MrDbUDZ6L7sTiRvQGVdmbA.png" /></figure><p>A high conversion rate indicates users register for recommended events more often. For example, a conversion rate of 0.30.3 means that users, on average, register for 3 events out of every 10 recommended events.</p><p><strong>Bookmark rate.</strong> A ratio showing how often users bookmark recommended events. This is based on the assumption that the platform allows users to save or bookmark an event.</p><p><strong>Revenue lift.</strong> This is the increase in revenue as a result of event recommendations.</p><h3>Serving</h3><p>In this section, we propose an ML system design that can be used to serve requests. As Figure 7.20 shows, there are two main pipelines in the design:</p><ul><li>Online learning pipeline</li><li>Prediction pipeline</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*ZNoqEWdGG4gxqC5lic5Q7w.png" /></figure><h4>Online learning pipeline</h4><p>As described earlier, event recommendations are intrinsically cold-start and suffer from a constant new-item problem. Consequently, the model must be continuously fine-tuned to adapt to new data. This pipeline is responsible for continuously training new models by incorporating new data, evaluating the trained models, and deploying them.</p><h4>Prediction pipeline</h4><p>The prediction pipeline is responsible for predicting the top kk most relevant events to a given user. Let’s discuss some of the most important components of the prediction pipeline.</p><p>Event filtering</p><p>The event filtering component takes the query user as input and narrows down the events from 1 million to a small subset of events. This is based upon simple rules, such as event locations, or other types of user filters. For example, if a user adds a “concerts only” filter, the component quickly narrows down the list to a subset of candidate events. Since these types of filters are common in event recommendation systems, they can be used to significantly reduce our search space from potentially millions of events, to hundreds of candidate events.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/501/1*zELT0R58W2H5JR4rKh3HLQ.png" /></figure><p>Ranking service</p><p>This service takes the user and candidate events produced by the filtering component as input, computes features for each ⟨⟨ user, event ⟩⟩ pair, sorts the events based on the probabilities predicted by the model, and outputs a ranked list of top k<em>k</em> most relevant events to the user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*_7EdAyKkl-o-A5BnbBUT1g.png" /></figure><p>Ranking service interacts with the feature computation component responsible for computing features that the model expects. Static features are obtained from a feature store, while dynamic features are computed in real-time from the raw data.</p><h3>Other Talking Points</h3><p>If there is extra time at the end of the interview, here are some additional talking points:</p><ul><li>What are the different types of bias we may observe in this system [21].</li><li>How to utilize feature crossing to achieve more expressiveness [22].</li><li>Some users like to see a diverse list of events. How to ensure the recommended events are diverse and fresh [23]?</li><li>We utilize the user’s attributes to train a model. We also rely on users’ live locations. What are additional considerations related to privacy and security [24]?</li><li>Event management platforms are usually two-sided marketplaces, where event hosts are the suppliers and users fulfill the demand side. How to ensure the system is not optimized for one side only? Additionally, how to keep the platform fair for different hosts? To learn more about unique challenges in two-sided marketplaces, refer to [25].</li><li>How to avoid data leakage when constructing the dataset [26].</li><li>How to determine the right frequency to update the models [27].</li></ul><h3>References</h3><ol><li>Learning to rank methods. <a href="https://livebook.manning.com/book/practical-recommender-systems/chapter-13/53">https://livebook.manning.com/book/practical-recommender-systems/chapter-13/53</a>.</li><li>RankNet paper. <a href="https://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf">https://icml.cc/2015/wp-content/uploads/2015/06/icml_ranking.pdf</a>.</li><li>LambdaRank paper. <a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/lambdarank.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/lambdarank.pdf</a>.</li><li>LambdaMART paper. <a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/MSR-TR-2010-82.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/MSR-TR-2010-82.pdf</a>.</li><li>SoftRank paper. <a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/SoftRankWsdm08Submitted.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/SoftRankWsdm08Submitted.pdf</a>.</li><li>ListNet paper. <a href="https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-2007-40.pdf">https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/tr-2007-40.pdf</a>.</li><li>AdaRank paper. <a href="https://dl.acm.org/doi/10.1145/1277741.1277809">https://dl.acm.org/doi/10.1145/1277741.1277809</a>.</li><li>Batch processing vs stream processing. <a href="https://www.confluent.io/learn/batch-vs-real-time-data-processing/#:~:text=Batch%20processing%20is%20when%20the,data%20flows%20through%20a%20system">https://www.confluent.io/learn/batch-vs-real-time-data-processing/#:~:text=Batch%20processing%20is%20when%20the,data%20flows%20through%20a%20system</a>.</li><li>Leveraging location data in ML systems. <a href="https://towardsdatascience.com/leveraging-geolocation-data-for-machine-learning-essential-techniques-192ce3a969bc#:~:text=Location%20data%20is%20an%20important,based%20on%20your%20customer%20data">https://towardsdatascience.com/leveraging-geolocation-data-for-machine-learning-essential-techniques-192ce3a969bc#:~:text=Location%20data%20is%20an%20important,based%20on%20your%20customer%20data</a>.</li><li>Logistic regression. <a href="https://www.youtube.com/watch?v=yIYKR4sgzI8">https://www.youtube.com/watch?v=yIYKR4sgzI8</a>.</li><li>Decision tree. <a href="https://careerfoundry.com/en/blog/data-analytics/what-is-a-decision-tree/">https://careerfoundry.com/en/blog/data-analytics/what-is-a-decision-tree/</a>.</li><li>Random forests. <a href="https://en.wikipedia.org/wiki/Random_forest">https://en.wikipedia.org/wiki/Random_forest</a>.</li><li>Bias/variance trade-off. <a href="http://www.cs.cornell.edu/courses/cs578/2005fa/CS578.bagging.boosting.lecture.pdf">http://www.cs.cornell.edu/courses/cs578/2005fa/CS578.bagging.boosting.lecture.pdf</a>.</li><li>AdaBoost. <a href="https://en.wikipedia.org/wiki/AdaBoost">https://en.wikipedia.org/wiki/AdaBoost</a>.</li><li>XGBoost. <a href="https://xgboost.readthedocs.io/en/stable/">https://xgboost.readthedocs.io/en/stable/</a>.</li><li>Gradient boosting. <a href="https://machinelearningmastery.com/gentle-introduction-gradient-boosting-algorithm-machine-learning/">https://machinelearningmastery.com/gentle-introduction-gradient-boosting-algorithm-machine-learning/</a>.</li><li>XGBoost in Kaggle competitions. <a href="https://www.kaggle.com/getting-started/145362">https://www.kaggle.com/getting-started/145362</a>.</li><li>GBDT. <a href="https://blog.paperspace.com/gradient-boosting-for-classification/">https://blog.paperspace.com/gradient-boosting-for-classification/</a>.</li><li>An introduction to GBDT. <a href="https://www.machinelearningplus.com/machine-learning/an-introduction-to-gradient-boosting-decision-trees/">https://www.machinelearningplus.com/machine-learning/an-introduction-to-gradient-boosting-decision-trees/</a>.</li><li>Introduction to neural networks. <a href="https://www.youtube.com/watch?v=i2fmaabIs5w">https://www.youtube.com/watch?v=i2fmaabIs5w</a>.</li><li>Bias issues and solutions in recommendation systems. <a href="https://www.youtube.com/watch?v=pPq9iyGIZZ8">https://www.youtube.com/watch?v=pPq9iyGIZZ8</a>.</li><li>Feature crossing to encode non-linearity. <a href="https://developers.google.com/machine-learning/crash-course/feature-crosses/encoding-nonlinearity">https://developers.google.com/machine-learning/crash-course/feature-crosses/encoding-nonlinearity</a>.</li><li>Freshness and diversity in recommendation systems. <a href="https://developers.google.com/machine-learning/recommendation/dnn/re-ranking">https://developers.google.com/machine-learning/recommendation/dnn/re-ranking</a>.</li><li>Privacy and security in ML. <a href="https://www.microsoft.com/en-us/research/blog/privacy-preserving-machine-learning-maintaining-confidentiality-and-preserving-trust/">https://www.microsoft.com/en-us/research/blog/privacy-preserving-machine-learning-maintaining-confidentiality-and-preserving-trust/</a>.</li><li>Two-sides marketplace unique challenges. <a href="https://www.uber.com/blog/uber-eats-recommending-marketplace/">https://www.uber.com/blog/uber-eats-recommending-marketplace/</a>.</li><li>Data leakage. <a href="https://machinelearningmastery.com/data-leakage-machine-learning/">https://machinelearningmastery.com/data-leakage-machine-learning/</a>.</li><li>Online training frequency. <a href="https://huyenchip.com/2022/01/02/real-time-machine-learning-challenges-and-solutions.html#towards-continual-learning">https://huyenchip.com/2022/01/02/real-time-machine-learning-challenges-and-solutions.html#towards-continual-learning</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d7f827b2ea1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning System Design Interview — Harmful Content Detection]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-harmful-content-detection-bf6b746c4b1a?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf6b746c4b1a</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Fri, 22 May 2026 19:56:00 GMT</pubDate>
            <atom:updated>2026-05-22T19:56:00.949Z</atom:updated>
            <content:encoded><![CDATA[<p>Many social media platforms such as Facebook [1], LinkedIn [2], and Twitter [3] have standard guidelines to enforce integrity and make their platforms safe for users. These guidelines prohibit certain user behaviors, activities, and content that are harmful to the community. It is essential to have technologies and resources in place to identify harmful content and bad actors. We can divide the focus of integrity enforcement into two categories:</p><ul><li>Harmful content: Posts that contain violence, nudity, self-harm, hate speech, etc.</li><li>Bad acts/bad actors: Fake accounts, spam, phishing, organized unethical activities, and other unsafe behaviors.</li></ul><p>In this chapter, we focus on detecting posts that might contain harmful content. In particular, we design a system that proactively monitors new posts, detects harmful content, and removes or demotes them if the content violates the platform’s guidelines. To understand how companies build a harmful content detection system in practice, refer to [4] [5] [6].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*kUUT61Q6JEyj9XF2blmLPw.png" /></figure><h3>Clarifying Requirements</h3><p>Here is a typical interaction between a candidate and an interviewer.</p><p><strong>Candidate:</strong> Does the system detect both harmful content and bad actors?<br><strong>Interviewer:</strong> Both are equally important. For simplicity, let’s focus on detecting harmful content only.</p><p><strong>Candidate:</strong> Should a post only contain text, or are images and videos allowed?<br><strong>Interviewer:</strong> The content of a post can be text, image, video, or any combination of these.</p><p><strong>Candidate:</strong> What languages are supported? Is it English only?<br><strong>Interviewer:</strong> The system should detect harmful content in various languages. For simplicity, assume we can use a pre-trained multilingual model to embed the textual content.</p><p><strong>Candidate</strong>: Which specific categories of harmful content are we looking to identify? I can think of violence, nudity, hate speech, misinformation, etc. Are there other harmful categories to consider?<br><strong>Interviewer:</strong> Great, you brought up the major ones. Misinformation is more complex and controversial. For simplicity, let’s not focus on misinformation.</p><p><strong>Candidate</strong>: Are there any human annotators available to label posts manually?<br><strong>Interviewer:</strong> The platform receives more than 500 million posts each day. Asking humans to label all of them would be very expensive and time-consuming. However, you can assume human annotation is available to label a limited number of posts, say 10,000 per day.</p><p><strong>Candidate:</strong> Allowing users to report harmful content is beneficial for understanding where the system is failing. Can I assume the system has that feature?<br><strong>Interviewer:</strong> Good point. Yes, users can report harmful posts.</p><p><strong>Candidate:</strong> Should we explain why a post is deemed harmful and removed?<br><strong>Interviewer:</strong> Yes. Explaining to users why we remove a post is essential. It helps users to ensure they align their future posts with the guidelines.</p><p><strong>Candidate:</strong> What is the system’s latency requirement? Do we need a real-time prediction, i.e., the system detects harmful content immediately and blocks it, or can we rely on batch prediction, i.e., detecting harmful content offline hourly or daily?<br><strong>Interviewer:</strong> This is a very important question. What are your thoughts?</p><p><strong>Candidate:</strong> In my opinion, the requirements for different harmful content might vary. For example, violent content may require real-time solutions, while for others, late detection may work.<br><strong>Interviewer:</strong> Those are fair assumptions.</p><p>So, let’s summarize the problem statement. We will design a harmful content detection system, which identifies harmful posts, then deletes or demotes them and informs the user why the post was identified as harmful. A post’s content can be text, image, video, or any combination of these, and the content can be in different languages. Users can report harmful posts.</p><h3>Frame the Problem as an ML Task</h3><h4>Defining the ML objective</h4><p>We define our ML objective as accurately predicting harmful posts. The reason is that if we can accurately detect harmful posts, we can remove or demote them, leading to a safer platform.</p><h4>Specifying the system’s input and output</h4><p>The system receives a post as input, and outputs the probability that the post is harmful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*S69aC5VFpJbZ8re6y-OAtQ.png" /></figure><p>Let’s dive deeper into the input post. As shown in Figure 5.3, a post can be heterogeneous and potentially multimodal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/293/1*P7fIC7YOioFdDTEks6obtQ.png" /></figure><p>To make accurate predictions, the system should consider all modalities. Let’s discuss two commonly used fusing methods to combine heterogeneous data: late fusion and early fusion.</p><p>Late fusion</p><p>With late fusion, ML models process different modalities independently, then combine their predictions to make a final prediction. The figure below illustrates how late fusion works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*wo8aho95RPoYPcQ9r4r7OQ.png" /></figure><p>The advantage of late fusion is that we can train, evaluate, and improve each model independently.</p><p>However, late fusion has two major disadvantages. First, to train these individual models, we need to have separate training data for each modality, which can be time-consuming and expensive.</p><p>Second, the combination of the modalities might be harmful, even if each is benign in isolation. This is frequently the case with memes that combine images and text. In these cases, late fusion fails to predict whether the content is harmful. This is because each modality is benign, so the models predict benignity when processing each modality. The fusion layer output is benign because the output of each separate modality is benign. But this is incorrect, as the combination of modalities can be harmful.</p><p>Early fusion</p><p>With early fusion, the modalities are combined first, and then the model makes a prediction. Figure 5.5 illustrates how early fusion works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*-SKxxrnWdLwlX72HpUvUQA.png" /></figure><p>Early fusion has two major advantages. First, it is unnecessary to collect training data separately for each modality. Since there is a single model to train, we only need to collect training data for that model. Second, the model considers all the modalities, so if each modality is benign, but their combination is harmful, then the model can potentially capture this in the unified feature vector.</p><p>However, learning this task is more difficult for the model due to the complex relationships between modalities. In the absence of sufficient training data, it is challenging for the model to learn complex relationships and make good predictions.</p><p>Which fusion method should we use?</p><p>The early fusion method is used because it allows us to capture posts that may be harmful overall, even if each modality is benign on its own. Additionally, with around 500 million posts being published every day, the model has enough data to learn the task.</p><h4>Choosing the right ML category</h4><p>In this section, we examine the following ML category options:</p><ul><li>Single binary classifier</li><li>One binary classifier per harmful class</li><li>Multi-label classifier</li><li>Multi-task classifier</li></ul><p>Single binary classifier</p><p>In this option, a model takes the fused features as input and predicts the probability of the post being harmful (Figure 5.6). Since the output is a binary outcome, the model is a binary classifier.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*BG8QU9VqUPHF9--sb59Y0A.png" /></figure><p>The shortcoming of this option is that it’s difficult to determine which class of harm, such as violence, a post belongs to. This limitation causes two main issues:</p><ul><li>It is not easy to inform users why we take down a post as the system only outputs a binary value indicating whether the post as a whole is harmful or not. We have no clue which particular class of harm the post belongs to.</li><li>It is not easy to identify harmful classes in which the system doesn’t perform well, meaning we cannot improve the system for underperforming classes.</li></ul><p>Since it is essential to explain why a post is removed, a single binary classifier is not a good option.</p><p>One binary classifier per harmful class</p><p>In this option, we adopt one binary classifier for each harmful class. As Figure 5.75.7 shows, each model determines if a post belongs to a specific harmful class or not. Each model takes fused features as input and predicts the probability of the post being classified as a harmful class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/345/1*iUA2P4aa9jBv0ySVEHNeNQ.png" /></figure><p>The advantage of this option is that we can explain to users why a post was taken down. In addition, we can monitor different models and improve them independently.</p><p>However, this option has one major drawback. Since we have multiple models, they must be trained and maintained separately. Training these models separately is time-consuming and expensive.</p><p>A multi-label classifier</p><p>In multi-label classification, the data point we want to classify may belong to an arbitrary number of classes. In this option, a single model is used as a multi-label classifier. As Figure 5.85.8 shows, the input to the model is the fused features, and the model predicts probabilities for each harmful class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*3qRAQETNohEVreKvmxwVOA.png" /></figure><p>By using a shared model for all harmful classes, training and maintaining the model is less costly. If you would like to learn more about this method, refer to the approach called WPIE [7].</p><p>However, predicting the probabilities of each harmful class using a shared model isn’t ideal, as the input features may need to be transformed differently.</p><p>Multi-task classifier</p><p>Multi-task learning refers to the process in which a model learns multiple tasks simultaneously. This allows the model to learn similarities between tasks. By doing so, we avoid unnecessary computations when a certain input transformation is beneficial for multiple tasks.</p><p>In our case, we treat different classes of harm, such as violence and nudity, as different tasks and use a multi-task classification model to learn each task. As Figure 5.9 shows, multi-task classification has two stages: shared layers and task-specific layers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*vEFf5DZxkXDlg5B2fItYcg.png" /></figure><p><strong>Shared layers</strong> A shared layer, as shown in Figure 5.10, is a set of hidden layers that transform input features into new ones. These newly transformed features are used to make predictions for each of the harmful classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*9ZfpBlrrrF-uvA3nSZJ8pw.png" /></figure><p><strong>Task-specific layers</strong> Task-specific layers are a set of independent ML layers (also called classification heads). Each classification head transforms features in a way that is optimal for predicting a specific harm probability.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*-InPgkRt7ZF35fq9kL6vUg.png" /></figure><p>Multi-task classification has three advantages. First, it is not expensive to train or maintain since we use a single model. Second, the shared layers transform the features in a way that is beneficial for each task. This prevents redundant computations and makes multi-task classification efficient. Lastly, the training data for each task contributes to the learning of other tasks. This is especially helpful when limited data is available for a particular task.</p><p>Because of these advantages, we employ a multi-task classification method. Figure 5.125.12 shows how we frame the problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*0VjkGNYcU5d7VcXlHu6wbA.png" /></figure><h3>Data Preparation</h3><h4>Data engineering</h4><p>We have the following data available:</p><ul><li>Users</li><li>Posts</li><li>User-post interactions</li></ul><p>Users</p><p>A user data schema is shown below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*1275fHfK2_nDSzyee3pJ9w.png" /></figure><p>Posts</p><p>Post data contain fields such as author, time of upload, etc. Table 5.25.2 shows some of the most important attributes. In practice, we typically have hundreds of attributes associated with each post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*AJbvggrOSO2nwaIrp1E4kg.png" /></figure><p>User-post interactions</p><p>User-post interaction data primarily contain users’ reactions to posts, such as likes, comments, saves, shares, etc. Users can also report a post as harmful or request an appeal. Table 5.35.3 shows what the data might look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/548/1*sKeQlpiukrvIocilXJJHEw.png" /></figure><h4>Feature engineering</h4><p>In the “Frame the problem as an ML task” section, we framed the problem as a multitask classification task where the input is the post. In this section, we explore predictive features that can be derived from a post. A post might comprise the following elements:</p><ul><li>Textual content</li><li>Image or video</li><li>User reactions to the post</li><li>Author</li><li>Contextual information</li></ul><p>Let’s look at each element.</p><p>Textual content</p><p>The textual content of a post can be used to determine whether a post is harmful or not. As described in Chapter 4 YouTube Video Search, text data is usually prepared in two steps:</p><ul><li>Text preprocessing (e.g., normalization, tokenization)</li><li>Vectorization: convert the preprocessed text into a meaningful feature vector Let’s focus on vectorization since it is unique to this chapter. To vectorize the text and extract a feature vector, statistical or ML-based methods can be used. Statistical methods such as BoW or TF-IDF are easy to implement and fast to compute. However, they cannot encode the semantics of the text. For our system, understanding the semantics of the textual content is important for determining harm, so we adopt the ML-based method. To convert text into a feature vector, we use a pre-trained Transformer-based language model such as BERT [8]. However, the original BERT has two issues:</li><li>Producing the text embedding takes a long time due to the large size of the model. Because this is a slow process, using it for online predictions is not ideal.</li><li>BERT was trained on English-only data. Thus, it does not produce meaningful embeddings for texts in other languages.</li></ul><p>DistilmBERT [9], a more efficient variant of BERT, addresses those two issues. If two sentences have the same meaning but are in two different languages, their embeddings are very similar. If you are interested to learn more about multilingual language models, refer to [10].</p><p>Image or video</p><p>You can usually find out what a post is about by looking at the image or video within it. The following two steps are commonly used to prepare unstructured data such as images or videos.</p><ul><li><strong>Preprocessing:</strong> decode, resize, and normalize the data.</li><li><strong>Feature extraction:</strong> after preprocessing, we use a pre-trained model to convert unstructured data to a feature vector. This allows us to represent an image or video by a feature vector. For images, a pre-trained image model such as CLIP’s visual encoder [11] or SimCLR [12] are viable options. For videos, pre-trained models like VideoMoCo [13] might work well.</li></ul><p>User reactions to the post</p><p>It is also possible to determine whether a post is harmful based on user reactions, especially when the content is ambiguous. As shown in Figure 5.13, with more comments, it becomes increasingly evident the post contains content related to self-harm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/666/1*KmChBKqBDnZUNJhw01tTTA.png" /></figure><p>Since user reactions are crucial in determining harmful content, let’s examine some of the features we can engineer based on them.</p><p><strong>The number of likes, shares, comments, and reports:</strong> We usually scale these numerical values to speed up convergence during model training.</p><p><strong>Comments:</strong> As demonstrated in Figure 5.13, comments can help us identify harmful content. For feature preparation, we convert comments into numerical representations by doing the following:</p><ul><li>Use the same pre-trained model we employed earlier to obtain the embedding of each comment.</li><li>Aggregate (e.g., average) the embeddings to obtain a final embedding.</li></ul><p>A summary of the features we have described so far can be found in Figure 5.14.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/773/1*nzmZgsyZsCa34aiNbQb1wg.png" /></figure><p>Author features</p><p>The author’s past interactions can be used to determine if the post is harmful or not. Let’s engineer features related to the post author.</p><p><strong>Author’s violation history</strong></p><ul><li><strong>Number of violations:</strong> This is a numerical value representing the number of times the author violated the guidelines in the past.</li><li><strong>Total user reports:</strong> A numerical value representing the number of times users reported the author’s posts.</li><li><strong>Profane words rate:</strong> This is a numerical value representing the rate of profane words used in the author’s previous posts and comments. A predefined list of profane words is used to determine whether a word is profane.</li></ul><p><strong>Author’s demographics</strong></p><ul><li><strong>Age:</strong> A user’s age is one of the most important predictive features.</li><li><strong>Gender:</strong> This categorical feature represents the user’s gender. We use one-hot encoding to represent gender.</li><li><strong>City and country:</strong> Both the city and country take many distinct values. To represent the features, we use an embedding layer to convert city and country into feature vectors. Note that one-hot encoding is not an efficient method to represent the city and country because their representations would be long and sparse.</li></ul><p><strong>Account information</strong></p><ul><li><strong>Number of followers and followings</strong></li><li><strong>Account age:</strong> This is a numerical value representing the age of the author’s account. This is a predictive feature as accounts with a lower age are more likely to be spam or to violate integrity.</li></ul><p>Contextual information</p><ul><li><strong>Time of day:</strong> This is the time of day when the author made a post. We bucketize this into multiple categories, such as morning, noon, afternoon, evening or night. We use one-hot encoding to represent this feature.</li><li><strong>Device:</strong> The device the author uses, such as a smartphone or desktop computer. One-hot encoding is used to represent this feature.</li></ul><p>Figure 5.15 summarizes some of the most important features of the harmful content detection system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*C2BMTfQjVkxRYWO0ooL3lQ.png" /></figure><h3>Model Development</h3><h4>Model selection</h4><p>A neural network is the most common model used for multi-task learning. In developing our model, we employ neural networks.</p><p>When choosing a neural network, what factors should be considered? It is necessary to determine the neural network’s architectural design and the optimal hyperparameter selection, such as its hidden layers, activation function, learning rate, etc. The optimal choice of hyperparameters is usually determined by hyperparameter tuning. Let’s briefly go over it.</p><p>Hyperparameter tuning is the process of finding the best values for hyperparameters in order to produce the best performance for a model. To tune hyperparameters, grid search is commonly used. The procedure involves training a new model for each combination of hyperparameter values, evaluating each model, then selecting the hyperparameters that lead to the best model. If you are interested in learning more about hyperparameter tuning, refer to [14].</p><h4>Model training</h4><p>Constructing the dataset</p><p>To train the multi-task classification model, we first need to construct the dataset. The dataset comprises model inputs (features) and outputs (labels) that the model is expected to predict. To construct inputs, we process posts offline in batches and compute fused features as described earlier. These features can be stored in a feature store for future training. In order to create labels for each input, we have two options:</p><ul><li>Hand labeling</li><li>Natural labeling</li></ul><p>With hand labeling, human contractors label posts manually. This option produces accurate labels, but it is expensive and time-consuming. With natural labeling, we rely on user reports to label posts automatically. While this option results in noisier labels, labels are produced more quickly. For the evaluation dataset, we use hand labeling to prioritize the accuracy of labels, and for the training dataset, we use natural labeling to prioritize labeling speed. A data point from the constructed dataset is shown in Figure 5.16.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/526/1*qOUXEHYwx27XboRtTGGcXw.png" /></figure><p>Choosing the loss function</p><p>Training a multi-task neural network is very similar to how we typically train neural network models. A forward propagation performs computations to make a prediction, a loss function measures the correctness of the prediction, and a backward propagation optimizes the model’s parameters to reduce the loss in the next iteration. Let’s examine the loss function. In multi-task training, each task is assigned a loss function based on its ML category. In our case, each task is framed as a binary classification, so we adopt a standard binary classification loss such as cross-entropy for each task. The overall loss is computed by combining task-specific losses, as shown in Figure 5.175.17.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/317/1*lQTfOsve__c4utKtpdFqyg.png" /></figure><p>A common challenge in training multimodal systems is overfitting [15]. For example, when the learning speed varies across different modalities, one modality (e.g., image) can dominate the learning process. Two techniques to address this issue are gradient blending and focal loss. If you are interested in learning more about these techniques, refer to [16] [17].</p><h3>Evaluation</h3><h4>Offline metrics</h4><p>To evaluate the performance of a binary classification model, offline metrics such as precision, recall, and f1 score are commonly used. However, precision or recall alone is not sufficient to understand the overall performance. For example, a model with high precision might have a very low recall. The precision-recall (PR) curve and receiver operating characteristic (ROC) curve address those limitations. Let’s explore each one.</p><p><strong>PR-curve.</strong> PR curve shows the trade-off between precision and recall of the model. As Figure 5.185.18 shows, we obtain a PR curve by plotting the precision of the model using different probability thresholds, ranging from 0 to 1 . To summarize the trade-offs between precision and recall, PR-AUC (the area under the precision-recall curve) calculates the area beneath the PR curve. In general, a high PR-AUC indicates a more accurate model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*L4WfT8sXxFpqh0YZoCVjzA.png" /></figure><p><strong>ROC curve.</strong> The ROC curve shows the trade-offs between the true positive rate (recall) and the false positive rate. Similar to the PR curve, ROC-AUC summarizes the model’s performance by calculating the area under the ROC curve.</p><p>ROC and PR curves are two different ways to summarize the performance of a classification model. To learn about the differences between the PR curve and the ROC curve, read [18].</p><p>In our case, we use both ROC-AUC and PR-AUC as our offline metrics.</p><h4>Online metrics</h4><p>Let’s explore a few important metrics to capture how safe the platform is.</p><p><strong>Prevalence.</strong> This metric measures the ratio of harmful posts which we didn’t prevent and all posts on the platform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*8XsiQ9kCXMimpXPuoBQfyg.png" /></figure><p>The shortcoming of this metric is that it treats harmful posts equally. For example, one harmful post with 100 K100 K views or impressions is more harmful than two posts with 10 views each.</p><p><strong>Harmful impressions.</strong> We prefer this metric over prevalence. The reason is that the number of harmful posts on the platform does not show how many people were affected by those posts, whereas the number of harmful impressions does capture this information.</p><p><strong>Valid appeals.</strong> Percentage of posts that were deemed harmful, but appealed and reversed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*hXKARIgS-s2zdbjws2cJ7w.png" /></figure><p><strong>Proactive rate.</strong> Percentage of harmful posts found and deleted by the system before users report it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/518/1*H2NfbbC_c5FHe3ug264dzQ.png" /></figure><p><strong>User reports per harmful class.</strong> This metric measures the system’s performance by looking into user reports for each harmful class.</p><h3>Serving</h3><p>Figure 5.19 shows the high-level ML system design. Let’s take a closer look at each component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*j1-dl0XeYviDJm_36GS2Sg.png" /></figure><h4>Harmful content detection service</h4><p>Given a new post, this service predicts the probability of harm. According to the requirements, some types of harm should be handled immediately due to their sensitivity. When this happens, the violation enforcement service removes the post immediately.</p><h4>Violation enforcement service</h4><p>The violation enforcement service immediately takes down a post if the harmful content detection service predicts harm with high confidence. It also notifies the user why the post was removed.</p><h4>Demoting service</h4><p>If the harmful content detection service predicts harm with low confidence, the demoting service temporarily demotes the post in order to decrease the chance of it spreading among users.</p><p>Then, the post is stored in storage for manual review by humans. The review team manually reviews the post and assigns a label from one of the predefined classes of harm. We will use these labeled posts in future training iterations to improve the model.</p><h4>Other Talking Points</h4><ul><li>Handle biases introduced by human labeling [19].</li><li>Adapt the system to detect trending harmful classes (e.g., Covid-19, elections) [20].</li><li>How to build a harmful content detection system that leverages temporal information such as users’ sequence of actions [21][22].</li><li>How to effectively select post samples for human review [23].</li><li>How to detect authentic and fake accounts [24].</li><li>How to deal with borderline contents [25], i.e., types of content that are not prohibited by guidelines, but come close to the red lines drawn by those policies.</li><li>How to make the harmful content detection system efficient, so we can deploy it on-device [26].</li><li>How to substitute Transformer-based architectures with linear Transformers to create a more efficient system [27] [28].</li></ul><h3>References</h3><ol><li>Facebook’s inauthentic behavior. <a href="https://transparency.fb.com/policies/community-standards/inauthentic-behavior/">https://transparency.fb.com/policies/community-standards/inauthentic-behavior/</a>.</li><li>LinkedIn’s professional community policies. <a href="https://www.linkedin.com/legal/professional-community-policies">https://www.linkedin.com/legal/professional-community-policies</a>.</li><li>Twitter’s civic integrity policy. <a href="https://help.twitter.com/en/rules-and-policies/election-integrity-policy">https://help.twitter.com/en/rules-and-policies/election-integrity-policy</a>.</li><li>Facebook’s integrity survey. <a href="https://arxiv.org/pdf/2009.10311.pdf">https://arxiv.org/pdf/2009.10311.pdf</a>.</li><li>Pinterest’s violation detection system. <a href="https://medium.com/pinterest-engineering/how-pinterest-fights-misinformation-hate-speech-and-self-harm-content-with-machine-learning-1806b73b40ef">https://medium.com/pinterest-engineering/how-pinterest-fights-misinformation-hate-speech-and-self-harm-content-with-machine-learning-1806b73b40ef</a>.</li><li>Abusive detection at LinkedIn. <a href="https://engineering.linkedin.com/blog/2019/isolation-forest">https://engineering.linkedin.com/blog/2019/isolation-forest</a>.</li><li>WPIE method. <a href="https://ai.facebook.com/blog/community-standards-report/">https://ai.facebook.com/blog/community-standards-report/</a>.</li><li>BERT paper. <a href="https://arxiv.org/pdf/1810.04805.pdf">https://arxiv.org/pdf/1810.04805.pdf</a>.</li><li>Multilingual DistilBERT. <a href="https://huggingface.co/distilbert-base-multilingual-cased">https://huggingface.co/distilbert-base-multilingual-cased</a>.</li><li>Multilingual language models. <a href="https://arxiv.org/pdf/2107.00676.pdf">https://arxiv.org/pdf/2107.00676.pdf</a>.</li><li>CLIP model. <a href="https://openai.com/blog/clip/">https://openai.com/blog/clip/</a>.</li><li>SimCLR paper. <a href="https://arxiv.org/pdf/2002.05709.pdf">https://arxiv.org/pdf/2002.05709.pdf</a>.</li><li>VideoMoCo paper. <a href="https://arxiv.org/pdf/2103.05905.pdf">https://arxiv.org/pdf/2103.05905.pdf</a>.</li><li>Hyperparameter tuning. <a href="https://cloud.google.com/ai-platform/training/docs/hyperparameter-tuning-overview">https://cloud.google.com/ai-platform/training/docs/hyperparameter-tuning-overview</a>.</li><li>Overfitting. <a href="https://en.wikipedia.org/wiki/Overfitting">https://en.wikipedia.org/wiki/Overfitting</a>.</li><li>Focal loss. <a href="https://amaarora.github.io/posts/2020-06-29-FocalLoss.html">https://amaarora.github.io/posts/2020-06-29-FocalLoss.html</a>.</li><li>Gradient blending in multimodal systems. <a href="https://arxiv.org/pdf/1905.12681.pdf">https://arxiv.org/pdf/1905.12681.pdf</a>.</li><li>ROC curve vs precision-recall curve. <a href="https://machinelearningmastery.com/roc-curves-and-precision-recall-curves-for-classification-in-python/">https://machinelearningmastery.com/roc-curves-and-precision-recall-curves-for-classification-in-python/</a>.</li><li>Introduced bias by human labeling. <a href="https://labelyourdata.com/articles/bias-in-machine-learning">https://labelyourdata.com/articles/bias-in-machine-learning</a>.</li><li>Facebook’s approach to quickly tackling trending harmful content. <a href="https://ai.facebook.com/blog/harmful-content-can-evolve-quickly-our-new-ai-system-adapts-to-tackle-it/">https://ai.facebook.com/blog/harmful-content-can-evolve-quickly-our-new-ai-system-adapts-to-tackle-it/</a>.</li><li>Facebook’s TIES approach. <a href="https://arxiv.org/pdf/2002.07917.pdf">https://arxiv.org/pdf/2002.07917.pdf</a>.</li><li>Temporal interaction embedding. <a href="https://www.facebook.com/atscaleevents/videos/730968530723238/">https://www.facebook.com/atscaleevents/videos/730968530723238/</a>.</li><li>Building and scaling human review system. <a href="https://www.facebook.com/atscaleevents/videos/1201751883328695/">https://www.facebook.com/atscaleevents/videos/1201751883328695/</a>.</li><li>Abusive account detection framework. <a href="https://www.youtube.com/watch?v=YeX4MdU0JNk">https://www.youtube.com/watch?v=YeX4MdU0JNk</a>.</li><li>Borderline contents. <a href="https://transparency.fb.com/features/approach-to-ranking/content-distribution-guidelines/content-borderline-to-the-community-standards/">https://transparency.fb.com/features/approach-to-ranking/content-distribution-guidelines/content-borderline-to-the-community-standards/</a>.</li><li>Efficient harmful content detection. <a href="https://about.fb.com/news/2021/12/metas-new-ai-system-tackles-harmful-content/">https://about.fb.com/news/2021/12/metas-new-ai-system-tackles-harmful-content/</a>.</li><li>Linear Transformer paper. <a href="https://arxiv.org/pdf/2006.04768.pdf">https://arxiv.org/pdf/2006.04768.pdf</a>.</li><li>Efficient AI models to detect hate speech. <a href="https://ai.facebook.com/blog/how-facebook-uses-super-efficient-ai-models-to-detect-hate-speech/">https://ai.facebook.com/blog/how-facebook-uses-super-efficient-ai-models-to-detect-hate-speech/</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf6b746c4b1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning System Design Interview — YouTube Video Search]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-youtube-video-search-6969cee4defc?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/6969cee4defc</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Thu, 21 May 2026 19:46:00 GMT</pubDate>
            <atom:updated>2026-05-21T19:46:00.623Z</atom:updated>
            <content:encoded><![CDATA[<p>On video-sharing platforms such as YouTube, the number of videos can quickly grow into the billions. In this chapter, we design a video search system that can efficiently handle this volume of content. As shown in Figure 4.1, the user enters text into the search box, and the system displays the most relevant videos for the given text.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*vFGsZig-ml_XdX1FcMrIcg.png" /></figure><h3>Clarifying Requirements</h3><p>Here is a typical interaction between a candidate and an interviewer.</p><p><strong>Candidate:</strong> Is the input query text-only, or can users search with an image or video?<br><strong>Interviewer:</strong> Text queries only.</p><p><strong>Candidate:</strong> Is the content on the platform only in video form? How about images or audio files?<br><strong>Interviewer:</strong> The platform only serves videos.</p><p><strong>Candidate:</strong> The YouTube search system is very complex. Can I assume the relevancy of a video is determined solely by its visual content and the textual data associated with the video, such as the title and description?<br><strong>Interviewer:</strong> Yes, that’s a fair assumption.</p><p><strong>Candidate:</strong> Is there any training data available?<br><strong>Interviewer:</strong> Yes, let’s assume we have ten million pairs of ⟨⟨ video, text query ⟩⟩.</p><p><strong>Candidate:</strong> Do we need to support other languages in the search system?<br><strong>Interviewer:</strong> For simplicity, let’s assume only English is supported.</p><p><strong>Candidate:</strong> How many videos are available on the platform?<br><strong>Interviewer:</strong> One billion videos.</p><p><strong>Candidate:</strong> Do we need to personalize the results? Should we rank the results differently for different users, based on their past interactions?<br><strong>Interviewer:</strong> As opposed to recommendation systems where personalization is essential, we do not necessarily have to personalize results in search systems. To simplify the problem, let’s assume no personalization is required.</p><p>Let’s summarize the problem statement. We are asked to design a search system for videos. The input is a text query, and the output is a list of videos that are relevant to the text query. To search for relevant videos, we leverage both the videos’ visual content and textual data. We are given a dataset of ten million ⟨⟨ video, text query ⟩⟩ pairs for model training.</p><h3>Frame the Problem as an ML Task</h3><h4>Defining the ML objective</h4><p>Users expect search systems to provide relevant and useful results. One way to translate this into an ML objective is to rank videos based on their relevance to the text query.</p><h4>Specifying the system’s input and output</h4><p>As shown in Figure 4.2, the search system takes a text query as input and outputs a ranked list of videos sorted by their relevance to the text query.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*l1F7LO6i-fdcJKvUf3oYQg.png" /></figure><h4>Choosing the right ML category</h4><p>In order to determine the relevance between a video and a text query, we utilize both visual content and the video’s textual data. An overview of the design can be seen in Figure 4.3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*Q0xsDKVnw-4E3qqljD4l4g.png" /></figure><p>Let’s briefly discuss each component.</p><p>Visual search</p><p>This component takes a text query as input and outputs a list of videos. The videos are ranked based on the similarity between the text query and the videos’ visual content.</p><p>Representation learning is a commonly used approach to search for videos by processing their visual content. In this approach, text query and video are encoded separately using two encoders. As shown in Figure 4.4, the ML model contains a video encoder that generates an embedding vector from the video, and a text encoder that generates an embedding vector from the text. The similarity score between the video and the text is calculated using the dot product of their representations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/331/1*MYIYmSKjzSdtJDN-ByaCqw.png" /></figure><p>In order to rank videos that are visually and semantically similar to the text query, we compute the dot product between the text and each video in the embedding space, then rank the videos based on their similarity scores.</p><p>Text search</p><p>Figure 4.5 shows how text search works when a user types in a text query: “dogs playing indoor”. Videos with the most similar titles, descriptions, or tags to the text query are shown as the output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*d3kq_5fPusCi03e9p2-4FQ.png" /></figure><p>The inverted index is a common technique for creating the text-based search component, allowing efficient full-text search in databases. Since inverted indexes aren’t based on machine learning, there is no training cost. A popular search engine companies often use is Elasticsearch, which is a scalable search engine and document store. For more details and a deeper understanding of Elasticsearch, refer to [1].</p><h3>Data Preparation</h3><h4>Data engineering</h4><p>Since we are given an annotated dataset to train and evaluate the model, it’s not necessary to perform any data engineering. Table 4.1 shows what the annotated dataset might look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*TQQfVJoewP58V5zs6cu4zQ.png" /></figure><h4>Feature engineering</h4><p>Almost all ML algorithms accept only numeric input values. Unstructured data such as texts and videos need to be converted into a numerical representation during this step. Let’s take a look at how to prepare the text and video data for the model.</p><p>Preparing text data</p><p>As shown in Figure 4.64.6, text is typically represented as a numerical vector using three steps: text normalization, tokenization, and tokens to IDs [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*xxUxhXVZPuqFUmXnqA8qaA.png" /></figure><p>Let’s take a look at each step in more detail.</p><p>Text normalization</p><p>Text normalization — also known as text cleanup — ensures words and sentences are consistent. For example, the same word may be spelled slightly differently; as in “dog”, “dogs”, and “DOG!” all refer to the same thing but are spelled in different ways. The same is true for sentences. Take these two sentences, for example:</p><ul><li>“A person walking with his dog in Montréal !”</li><li>“a person walks with his dog, in Montreal.”</li></ul><p>Both sentences mean the same, but have differing punctuation and verb forms. Here are some typical methods for text normalization:</p><ul><li>Lowercasing: make all letters lowercase, as this does not change the meaning of words or sentences</li><li>Punctuation removal: remove punctuation from the text. Common punctuation marks are the period, comma, question mark, exclamation point, etc.</li><li>Trim whitespaces: trim leading, trailing, and multiple whitespaces</li><li>Normalization Form KD (NFKD) [3]: decompose combined graphemes into a combination of simple ones</li><li>Strip accents: remove accent marks from words. For example: Màlaga →→ Malaga, Noël →→ Noel</li><li>Lemmatization and stemming: identify a canonical representative for a set of related word forms. For example: walking, walks, walked →→ walk</li></ul><p>Tokenization</p><p>Tokenization is the process of breaking down a piece of text into smaller units called tokens. Generally, there are three types of tokenization:</p><ul><li>Word tokenization: split the text into individual words based on specific delimiters. For example, a phrase like “I have an interview tomorrow” becomes [ “I”, “have”, “an”, “interview”, “tomorrow”]</li><li>Subword tokenization: split text into subwords (or n-gram characters)</li><li>Character tokenization: split text into a set of characters The details of different tokenization algorithms are not usually a strong focus in ML system design interviews. If you are interested to learn more, refer to [4].</li></ul><p>Tokens to IDs</p><p>Once we have the tokens, we need to convert them to numerical values (IDs). The representation of tokens with numerical values can be done in two ways:</p><ul><li>Lookup table</li><li>Hashing</li></ul><p><strong>Lookup table.</strong> In this method, each unique token is mapped to an ID. Next, a lookup table is created to store these 1:11:1 mappings. Figure 4.74.7 shows what the mapping table might look like.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/209/1*HdjGweWHiIg1RVKqPDikqw.png" /></figure><p><strong>Hashing.</strong> Hashing, also called “feature hashing” or “hashing trick,” is a memory-efficient method that uses a hash function to obtain IDs, without keeping a lookup table. Figure 4.84.8 shows how a hash function is used to convert words to IDs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*pPgmZEWOk8r3GcnP1S0PPA.png" /></figure><p>Let’s compare the lookup table with the hashing method.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/792/1*P8cvjwyALpaF4120RcynhA.png" /></figure><p>Preparing video data</p><p>Figure 4.9 shows a typical workflow for preprocessing a raw video.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/469/1*pYc7oTZJJ52r6EY-FODe2g.png" /></figure><h3>Model Development</h3><h4>Model selection</h4><p>As discussed in the “Framing the problem as an ML task” section, text queries are converted into embeddings by a text encoder, and videos are converted into embeddings by a video encoder. In this section, we examine possible model architectures for each encoder.</p><p>A typical text encoder’s input and output are shown in Figure 4.10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*isheQ1VOSdES9fNkiZsPYw.png" /></figure><p>The text encoder converts text into a vector representation [6]. For example, if two sentences have similar meanings, their embeddings are more similar. To build the text encoder, two broad categories are available: statistical methods and ML-based methods. Let’s examine each.</p><p>Statistical methods</p><p>Those methods rely on statistics to convert a sentence into a feature vector. Two popular statistical methods are:</p><ul><li>Bag of Words (BoW)</li><li>Term Frequency Inverse Document Frequency (TF-IDF)</li></ul><p><strong>BoW.</strong> This method converts a sentence into a fixed-length vector. It models sentenceword occurrences by creating a matrix with rows representing sentences, and columns representing word indices. An example of BoW is shown in Figure 4.11.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*5r8i8s7cGXBGMldJKH5_Lg.png" /></figure><p>BoW is a simple method that computes sentence representations fast, but has the following limitations:</p><ul><li>It does not consider the order of words in a sentence. For example, “let’s watch TV after work” and “let’s work after watch TV” would have the same BoW representation.</li><li>The obtained representation does not capture the semantic and contextual meaning of the sentence. For example, two sentences with the same meaning but different words have a totally different representation.</li><li>The representation vector is sparse. The size of the representation vector is equal to the total number of unique tokens we have. This number is usually very large, so each sentence representation is mostly filled with zeros.</li></ul><p><strong>TF-IDF.</strong> This is a numerical statistic intended to reflect how important a word is to a document in a collection or corpus. TF-IDF creates the same sentence-word matrix as in BoW, but it normalizes the matrix based on the frequency of words. To learn more about the mathematics behind this, refer to [7].</p><p>Since TF-IDF gives less weight to frequent words, its representations are usually better than BoW. However, it has the following limitations:</p><ul><li>A normalization step is needed to recompute term frequencies when a new sentence is added.</li><li>It does not consider the order of words in a sentence.</li><li>The obtained representation does not capture the semantic meaning of the sentence.</li><li>The representations are sparse.</li></ul><p>In summary, statistical methods are usually fast. However, they do not capture the contextual meaning of sentences, and the representations are sparse. ML-based methods address those issues.</p><p>ML-based methods</p><p>In these methods, an ML model converts sentences into meaningful word embeddings so that the distance between two embeddings reflects the semantic similarity of the corresponding words. For example, if two words, such as “rich” and “wealth” are semantically similar, their embeddings are close in the embedding space. Figure 4.124.12 shows a simple visualization of word embeddings in the 2D2D embedding space. As you can see, similar words are grouped together.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*iuIG7dpR_rjhRoU9qVtpcA.png" /></figure><p>There are three common ML-based approaches for transforming texts into embeddings:</p><ul><li>Embedding (lookup) layer</li><li>Word2vec</li><li>Transformer-based architectures</li></ul><p><strong>Embedding (lookup) layer</strong> In this approach, an embedding layer is employed to map each ID to an embedding vector. Figure 4.13 shows an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*R4EtNHAFwSKvYvgjcW9b0A.png" /></figure><p>Employing an embedding layer is a simple and effective solution to convert sparse features, such as IDs, into a fixed-size embedding. We will see more examples of its usage in later chapters.</p><p><strong>Word2vec</strong> Word2vec [8] is a family of related models used to produce word embeddings. These models use a shallow neural network architecture and utilize the co-occurrences of words in a local context to learn word embeddings. In particular, the model learns to predict a center word from its surrounding words during the training phase. After the training phase, the model is capable of converting words into meaningful embeddings.</p><p>There are two main models based on word2vec: Continuous Bag of Words (CBOW) [9] and Skip-gram [10]. Figure 4.144.14 shows how CBOW works at a high level. If you are interested to learn about these models, refer to [8].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*Gs9Jk731ud84oWPlGW6gDA.png" /></figure><p>Even though word2vec and embedding layers are simple and effective, recent architectures based upon Transformers have shown promising results.</p><p><strong>Transformer-based models</strong><br>These models consider the context of the words in a sentence when converting them into embeddings. As opposed to word2vec models, they produce different embeddings for the same word depending on the context.</p><p>Figure 4.15 shows a Transformer-based model which takes a sentence — a set of words as input, and produces an embedding for each word.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*jgm_KUNdCZiN64c1xDXpLg.png" /></figure><p>Transformers are very powerful at understanding the context and producing meaningful embeddings. Several models, such as BERT [11], GPT3 [12], and BLOOM [13], have demonstrated Transformers’ potential to perform a wide variety of Natural Language Processing (NLP) tasks. In our case, we choose a Transformer-based architecture such as BERT as our text encoder.</p><p>In some interviews, the interviewer may want you to dive deeper into the details of the Transformer-based model. To learn more, refer to [14].</p><p>Video encoder</p><p>We have two architectural options for encoding videos: We have two architectural options for encoding videos:</p><ul><li>Video-level models</li><li>Frame-level models</li></ul><p>Video-level models process a whole video to create an embedding, as shown in Figure 4.16. The model architecture is usually based on 3D convolutions [15] or Transformers. Since the model processes the whole video, it is computationally expensive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*qBCSHFI4VWirEbvylW_0_A.png" /></figure><p>Frame-level models work differently. It is possible to extract the embedding from a video using a frame-level model by breaking it down into three steps:</p><ul><li>Preprocess a video and sample frames.</li><li>Run the model on the sampled frames to create frame embeddings.</li><li>Aggregate (e.g., average) frame embeddings to generate the video embedding.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*oM4M04DkWA1AzMD4ejMdsw.png" /></figure><p>Since this model works at the frame level, it is often faster and computationally less expensive. However, frame-level models are usually not able to understand the temporal aspects of the video, such as actions and motions. In practice, frame-level models are preferred in many cases where a temporal understanding of the video is not crucial. Here, we employ a frame-level model such as ViT [16] for two reasons:</p><ul><li>Improve the training and serving speed</li><li>Reduce the number of computations</li></ul><h4>Model training</h4><p>To train the text encoder and video encoder, we use a contrastive learning approach. If you are interested in learning more about this, see the “Model training” section in Chapter 2, Visual Search System.</p><p>An explanation of how to compute the loss during model training is shown in Figure 4.18.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*vI3X7X1kwnJ1i-KFl5VvVQ.png" /></figure><h3>Evaluation</h3><h4>Offline metrics</h4><p>Here are some offline metrics that are typically used in search systems. Let’s examine which are the most relevant.</p><p><strong>Precision@k and mAP</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*NdaIBa68Zher3y_nrP_0eg.png" /></figure><p>In the evaluation dataset, a given text query is associated with only one video. That means the numerator of the precision@k formula is at most 1. This leads to low precison@k values. For example, for a given text query, even if we rank its associated video at the top of the list, the precision@10 is only 0.1. Due to this limitation, precision metrics, such as precision@k and mAP, are not very helpful.</p><p><strong>Recall@k.</strong> This measures the ratio between the number of relevant videos in the search results and the total number of relevant videos.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*7FCE7AYMYT8UtGJ-LcgQcQ.png" /></figure><p>As described earlier, the “total number of relevant videos” is always 1 . With that, we can translate the recall@k formula to the following:</p><p>recall@ k=1k=1 if the relevant video is among the top k<em>k</em> videos, 0 otherwise</p><p>What are the pros and cons of this metric?</p><p><strong>Pros</strong></p><ul><li>It effectively measures a model’s ability to find the associated video for a given text query.</li></ul><p><strong>Cons</strong></p><ul><li>It depends on k<em>k</em>. Choosing the right kk could be challenging.</li><li>When the relevant video is not among the kk videos in the output list, recall@k is always 0. For example, consider the case where model A ranks a relevant video at place 15, and model B ranks the same video at place 50. If we use recall@10 to measure the quality of these two models, both would have recall@10=0, even though model A is better than model B.</li></ul><p><strong>Mean Reciprocal Rank (MRR).</strong> This metric measures the quality of the model by averaging the rank of the first relevant item in each search result. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/153/1*oju3LAc368BLqq1t1hmL3w.png" /></figure><p>This metric addresses the shortcomings of recall@k and can be used as our offline metric.</p><h4>Online metrics</h4><p>As part of online evaluation, companies track a wide variety of metrics. Let’s take a look at some of the most important ones:</p><ul><li>Click-through rate (CTR)</li><li>Video completion rate</li><li>Total watch time of search results</li></ul><p><strong>CTR.</strong> This metric shows how often users click on retrieved videos. The main problem with CTR is that it does not track whether the clicked videos are relevant to the user. In spite of this issue, CTR is still a good metric to track because it shows how many people clicked on search results.</p><p><strong>Video completion rate.</strong> A metric measuring how many videos appear in search results and are watched by users until the end. The problem with this metric is that a user may watch a video only partially, but still find it relevant. The video completion rate alone cannot reflect the relevance of search results.</p><p><strong>Total watch time of search results.</strong> This metric tracks the total time users spent watching the videos returned by the search results. Users tend to spend more time watching if the search results are relevant. This metric is a good indication of how relevant the search results are.</p><h3>Serving</h3><p>At serving time, the system displays a ranked list of videos relevant to a given text query. Figure 4.194.19 shows a simplified ML system design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*mzUnDSWLpQrFkG_uSI4WFg.png" /></figure><p>Let’s discuss each pipeline in more detail.</p><h4>Prediction pipeline</h4><p>This pipeline consists of:</p><ul><li>Visual search</li><li>Text search</li><li>Fusing layer</li><li>Re-ranking service</li></ul><p>Visual search. This component encodes the text query and uses the nearest neighbor service to find the most similar video embeddings to the text embedding. To accelerate the NN search, we use approximate nearest neighbor (ANN) algorithms, as described in Chapter 2, Visual Search System.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*fBusgsZYllOoV9hH8CPq9w.png" /></figure><p><strong>Text search.</strong> Using Elasticsearch, this component finds videos with titles and tags that overlap the text query.</p><p><strong>Fusing layer.</strong> This component takes two different lists of relevant videos from the previous step, and combines them into a new list of videos.</p><p>The fusing layer can be implemented in two ways, the easiest of which is to re-rank videos based on the weighted sum of their predicted relevance scores. A more complex approach is to adopt an additional model to re-rank the videos, which is more expensive because it requires model training. Additionally, it’s slower at serving. As a result, we use the former approach.</p><p><strong>Re-ranking service.</strong> This service modifies the ranked list of videos by incorporating business-level logic and policies.</p><h4>Video indexing pipeline</h4><p>A trained video encoder is used to compute video embeddings, which are then indexed. These indexed video embeddings are used by the nearest neighbor service.</p><h4>Text indexing pipeline</h4><p>This uses Elasticsearch for indexing titles, manual tags, and auto-generated tags.</p><p>Usually, when a user uploads a video, they provide tags to help better identify the video. But what if they do not manually enter tags? One option is to use a standalone model to generate tags. We name this component the auto-tagger and it is especially valuable in cases where a video has no manual tags. These tags may be noisier than manual tags, but they are still valuable.</p><h3>Other Talking Points</h3><p>Before concluding this chapter, it’s important to note we have simplified the system design of the video search system. In practice, it is much more complex. Some improvements may include:</p><ul><li>Use a multi-stage design (candidate generation + ranking).</li><li>Use more video features such as video length, video popularity, etc.</li><li>Instead of relying on annotated data, use interactions (e.g., clicks, likes, etc.) to construct and label data. This allows us to continuously train the model.</li><li>Use an ML model to find titles and tags which are semantically similar to the text query. This model can be combined with Elasticsearch to improve search quality.</li></ul><p>If there’s time left at the end of the interview, here are some additional talking points:</p><ul><li>An important topic in search systems is query understanding, such as spelling correction, query category identification, and entity recognition. How to build a queryunderstanding component? [17].</li><li>How to build a multi-modal system that processes speech and audio to improve search results [18].</li><li>How to extend this work to support other languages [19].</li><li>Near-duplicate videos in the final output may negatively impact user experience. How to detect near-duplicate videos so we can remove them before displaying the results [20][20] ?</li><li>Text queries can be divided into head, torso, and tail queries. What are the different approaches commonly used in each case [21]?</li><li>How to consider popularity and freshness when producing the output list [22]?</li><li>How real-world search systems work [23][24][25].</li></ul><h3>References</h3><ol><li>Elasticsearch. <a href="https://www.tutorialspoint.com/elasticsearch/elasticsearch_query_dsl.htm">https://www.tutorialspoint.com/elasticsearch/elasticsearch_query_dsl.htm</a>.</li><li>Preprocessing text data. <a href="https://huggingface.co/docs/transformers/v4.42.0/preprocessing">https://huggingface.co/docs/transformers/v4.42.0/preprocessing</a>.</li><li>NFKD normalization. <a href="https://unicode.org/reports/tr15/">https://unicode.org/reports/tr15/</a>.</li><li>What is Tokenization summary. <a href="https://huggingface.co/docs/transformers/tokenizer_summary">https://huggingface.co/docs/transformers/tokenizer_summary</a>.</li><li>Hash collision. <a href="https://en.wikipedia.org/wiki/Hash_collision">https://en.wikipedia.org/wiki/Hash_collision</a>.</li><li>Deep learning for NLP. <a href="http://cs224d.stanford.edu/lecture_notes/notes1.pdf">http://cs224d.stanford.edu/lecture_notes/notes1.pdf</a>.</li><li>TF-IDF. <a href="https://en.wikipedia.org/wiki/Tf%E2%80%93idf">https://en.wikipedia.org/wiki/Tf%E2%80%93idf</a>.</li><li>Word2Vec models. <a href="https://www.tensorflow.org/tutorials/text/word2vec">https://www.tensorflow.org/tutorials/text/word2vec</a>.</li><li>Continuous bag of words. <a href="https://www.kdnuggets.com/2018/04/implementing-deep-learning-methods-feature-engineering-text-data-cbow.html">https://www.kdnuggets.com/2018/04/implementing-deep-learning-methods-feature-engineering-text-data-cbow.html</a>.</li><li>Skip-gram model. <a href="http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/">http://mccormickml.com/2016/04/19/word2vec-tutorial-the-skip-gram-model/</a>.</li><li>BERT model. <a href="https://arxiv.org/pdf/1810.04805.pdf">https://arxiv.org/pdf/1810.04805.pdf</a>.</li><li>GPT3 model. <a href="https://arxiv.org/pdf/2005.14165.pdf">https://arxiv.org/pdf/2005.14165.pdf</a>.</li><li>BLOOM model. <a href="https://bigscience.huggingface.co/blog/bloom">https://bigscience.huggingface.co/blog/bloom</a>.</li><li>Transformer implementation from scratch. <a href="https://peterbloem.nl/blog/transformers">https://peterbloem.nl/blog/transformers</a>.</li><li>3D convolutions. <a href="https://www.kaggle.com/code/shivamb/3d-convolutions-understanding-use-case/notebook">https://www.kaggle.com/code/shivamb/3d-convolutions-understanding-use-case/notebook</a>.</li><li>Vision Transformer. <a href="https://arxiv.org/pdf/2010.11929.pdf">https://arxiv.org/pdf/2010.11929.pdf</a>.</li><li>Query understanding for search engines. <a href="https://www.linkedin.com/pulse/ai-query-understanding-daniel-tunkelang/">https://www.linkedin.com/pulse/ai-query-understanding-daniel-tunkelang/</a>.</li><li>Multimodal video representation learning. <a href="https://arxiv.org/pdf/2012.04124.pdf">https://arxiv.org/pdf/2012.04124.pdf</a>.</li><li>Multilingual language models. <a href="https://arxiv.org/pdf/2107.00676.pdf">https://arxiv.org/pdf/2107.00676.pdf</a>.</li><li>Near-duplicate video detection. <a href="https://arxiv.org/pdf/2005.07356.pdf">https://arxiv.org/pdf/2005.07356.pdf</a>.</li><li>Generalizable search relevance. <a href="https://livebook.manning.com/book/ai-powered-search/chapter-10/v-10/20">https://livebook.manning.com/book/ai-powered-search/chapter-10/v-10/20</a>.</li><li>Freshness in search and recommendation systems. <a href="https://developers.google.com/machine-learning/recommendation/dnn/re-ranking">https://developers.google.com/machine-learning/recommendation/dnn/re-ranking</a>.</li><li>Semantic product search by Amazon. <a href="https://arxiv.org/pdf/1907.00937.pdf">https://arxiv.org/pdf/1907.00937.pdf</a>.</li><li>Ranking relevance in Yahoo search. <a href="https://www.kdd.org/kdd2016/papers/files/adf0361-yinA.pdf">https://www.kdd.org/kdd2016/papers/files/adf0361-yinA.pdf</a>.</li><li>Semantic product search in E-Commerce. <a href="https://arxiv.org/pdf/2008.08180.pdf">https://arxiv.org/pdf/2008.08180.pdf</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6969cee4defc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning System Design Interview — Google Street View Blurring System]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-google-street-view-blurring-system-98e376451490?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/98e376451490</guid>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Thu, 21 May 2026 19:36:00 GMT</pubDate>
            <atom:updated>2026-05-21T19:36:00.704Z</atom:updated>
            <content:encoded><![CDATA[<p>Google Street View [1] is a technology in Google Maps that provides street-level interactive panoramas of many public road networks around the world. In 2008, Google created a system that automatically blurs human faces and license plates to protect user privacy. In this chapter, we design a blurring system similar to Google Street View.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*5utyZECifBQhugiMmX7Ayw.png" /></figure><h3>Clarifying Requirements</h3><p>Here’s a typical conversation between a candidate and the interviewer.</p><p><strong>Candidate:</strong> Is it fair to say the business objective of the system is to protect user privacy?<br><strong>Interviewer:</strong> Yes.</p><p><strong>Candidate:</strong> We want to design a system that detects all human faces and license plates in Street View images and blurs them before displaying them to users. Is that correct? Can I assume users can report images that are not correctly blurred?<br><strong>Interviewer:</strong> Yes, those are fair assumptions.</p><p><strong>Candidate:</strong> Do we have an annotated dataset for this task?<br><strong>Interviewer:</strong> Let’s assume we have sampled 1 million images. Human faces and license plates are manually annotated in those images.</p><p><strong>Candidate:</strong> The dataset may not contain faces from certain racial profiles, which may cause a bias towards certain human attributes such as race, age, gender, etc. Is that a fair assumption?<br><strong>Interviewer:</strong> Great point. For simplicity, let’s not focus on fairness and bias today.</p><p><strong>Candidate:</strong> My understanding is that latency is not a big concern, as the system can detect objects and blur them offline. Is that correct?<br><strong>Interviewer:</strong> Yes. We can display existing images to users while new ones are being processed offline.</p><p>Let’s summarize the problem statement. We want to design a Street View blurring system that automatically blurs license plates and human faces. We are given a training dataset of 1 million images with annotated human faces and license plates. The business objective of the system is to protect user privacy.</p><h3>Frame the Problem as an ML Task</h3><p>In this section, we frame the problem as an ML task.</p><h4>Defining the ML objective</h4><p>The business objective of this system is to protect user privacy by blurring visible license plates and human faces in Street View images. But protecting user privacy is not an ML objective, so we need to translate it into an ML objective that an ML system can solve. One possible ML objective is to accurately detect objects of interest in an image. If an ML system can detect those objects accurately, then we can blur the objects before displaying the images to users.</p><p>Throughout this chapter, we use “objects” instead of “human faces and license plates” for conciseness.</p><h4>Specifying the system’s input and output</h4><p>The input of an object detection model is an image with zero or multiple objects at different locations within it. The model detects those objects and outputs their locations. Figure 3.2 shows an object detection system, along with its input and output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*9dbQVBAokRySkPARw2XPyA.png" /></figure><h4>Choosing the right ML category</h4><p>In general, an object detection system has two responsibilities:</p><ul><li>Predicting the location of each object in the image</li><li>Predicting the class of each bounding box (e.g., dog, cat, etc.)</li></ul><p>The first task is a regression problem since the location can be represented by (x,y)(<em>x</em>,<em>y</em>) coordinates, which are numeric values. The second task can be framed as a multi-class classification problem.</p><p>Traditionally, object detection architectures are divided into one-stage and two-stage networks. Recently, Transformer-based architectures such as DETR [2] have shown promising results, but in this chapter, we mainly explore two-stage and one-stage architectures.</p><h4>Two-stage networks</h4><p>As the name implies, two separate models are used in two-stage networks:</p><ol><li><strong>Region proposal network (RPN):</strong> scans an image and proposes candidate regions that are likely to be objects.</li><li><strong>Classifier:</strong> processes each proposed region and classifies it into an object class.</li></ol><p>Figure 3.3 shows these two stages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*CZUD2M_33hxzfSdj2qVrdw.png" /></figure><p>Commonly used two-stage networks include: R-CNN [3], Fast R-CNN [4], and FasterRCNN [5].</p><h4>One-stage networks</h4><p>In these networks, both stages are combined. Using a single network, bounding boxes and object classes are generated simultaneously, without explicit detection of region proposals. Figure 3.4 shows a one-stage network.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*FFKIdBXL77SyRFoQBpd3eQ.png" /></figure><p>Commonly used one-stage networks include: YOLO [6] and SSD [7] architectures.</p><p>One-stage vs. two-stage</p><p>Two-stage networks comprise two components that run sequentially, so they are usually slower, but more accurate.</p><p>In our case, the dataset contains 1 million images, which is not huge by modern standards. This indicates that using a two-stage network doesn’t increase the training cost excessively. So, for this exercise, we start with a two-stage network. When training data increases or predictions need to be made faster, we can switch to one-stage networks.</p><h3>Data Preparation</h3><h4>Data engineering</h4><p>In the Introduction chapter, we discussed data engineering fundamentals. Additionally, it’s usually a good idea to discuss the specific data available for the task at hand. For this problem, we have the following data available:</p><ul><li>Annotated dataset</li><li>Street View images</li></ul><p>Let’s discuss each in more detail.</p><p>Annotated dataset</p><p>Based on the requirements, we have 1 million annotated images. Each image has a list of bounding boxes and associated object classes. Table 3.1 shows data points from the dataset:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/528/1*DPeFWv-yx1hoLETnlEG9Yg.png" /></figure><p>Each bounding box is a list of 4 numbers: top left X and Y coordinates, followed by the width and height of the object.</p><p>Street View images</p><p>These are the Street View images collected by the data sourcing team. The ML system processes these images to detect human faces and license plates. Table 3.2 shows the metadata of the images.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*VnloBovI2rTXng10elA34g.png" /></figure><h4>Feature engineering</h4><p>During feature engineering, we first apply standard , such as resizing and normalization. After that, we increase the size of the dataset by using a data augmentation technique. Let’s take a closer look at this.</p><p>Data augmentation</p><p>A technique called data augmentation involves adding slightly modified copies of original data, or creating new data artificially from the original. As the dataset size increases, the model is able to learn more complex patterns. This technique is especially useful when the dataset is imbalanced, as it increases the number of data points in minority classes.</p><p>A special type of data augmentation is image augmentation. Among the commonly used augmentation techniques are:</p><ul><li>Random crop</li><li>Random saturation</li><li>Vertical or horizontal flip</li><li>Rotation and/or translation</li><li>Affine transformations</li><li>Changing brightness, saturation, or contrast</li></ul><p>Figure 3.5 shows an image with various data augmentation techniques applied to it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*Er_xy74Z968EHH2TmB1GWA.png" /></figure><p>It is important to note that with certain types of augmentations, the ground truth bounding boxes also need to be transformed. For example, when rotating or flipping the original image, the ground truth bounding boxes must also be transformed.</p><p>Data augmentation is used in offline or online forms.</p><ul><li><strong>Offline:</strong> Augment images before training</li><li><strong>Online:</strong> Augment images on the fly during training</li></ul><p><strong>Online vs. offline:</strong> In offline data augmentation, training is faster since no additional augmentation is needed. However, it requires additional storage to store all the augmented images. While online data augmentation slows down training, it does not consume additional storage.</p><p>The choice between online and offline data augmentation depends upon the storage and computing power constraints. What is more important in an interview is that you talk about different options and discuss trade-offs. In our case, we perform offline data augmentation.</p><p>Figure 3.6 shows the dataset preparation flow. With preprocessing, images are resized, scaled, and normalized. With image augmentation, the number of images is increased. Let’s say the number increases from 1 million to 10 million.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*yBEjyza_49SA8PYuy9HCpA.png" /></figure><h3>Model Development</h3><h4>Model selection</h4><p>As mentioned in the “Frame the Problem as an ML Task” section, we opt for two-stage networks. Figure 3.7 shows a typical two-stage architecture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*o0SdlHj7eZvEWwHHphbJWA.png" /></figure><p>Let’s examine each component.</p><p>Convolutional layers</p><p>Convolutional layers [9] process the input image and output a feature map.</p><p>Region Proposal Network (RPN)</p><p>RPN proposes candidate regions that may contain objects. It uses neural networks as its architecture and takes the feature map produced by convolutional layers as input and outputs candidate regions in the image.</p><p>Classifier</p><p>The classifier determines the object class of each candidate region. It takes the feature map and the proposed candidate regions as input, and assigns an object class to each region. This classifier is usually based on neural networks.</p><p>In ML system design interviews, you are generally not expected to discuss the architecture of these neural networks.</p><h4>Model training</h4><p>The process of training a neural network usually involves three steps: forward propagation, loss calculation, and backward propagation. Readers are expected to be familiar with these steps, but for more information, see [10]. In this section, we discuss the loss functions commonly used to detect objects.</p><p>An object detection model is expected to perform two tasks well. First, the bounding boxes of the objects predicted should have a high overlap with the ground truth bounding boxes. This is a regression task. Second, the predicted probabilities for each object class should be accurate. This is a classification task. Let’s define a loss function for each.</p><p><strong>Regression loss:</strong> This loss measures how aligned the predicted bounding boxes are with the ground truth bounding boxes. We use standard regression loss functions, such as Mean Squared Error (MSE) [11], and denote it by Lreg<em>Lreg</em>​ :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*9iY-z3XhS4--PBcnCaKPIA.png" /></figure><p>Where:</p><ul><li>M<em>M</em> : total number of predictions</li><li>xi<em>xi</em>​ : ground truth top left x<em>x</em> coordinate</li><li>x^i<em>x</em>^<em>i</em>​ : predicted top left x<em>x</em> coordinate</li><li>yi<em>yi</em>​ : ground truth top left y<em>y</em> coordinate</li><li>y^i<em>y</em>^​<em>i</em>​ : predicted top left y<em>y</em> coordinate</li><li>wi<em>wi</em>​ : ground truth width</li><li>w^i:<em>w</em>^<em>i</em>​: predicted width</li><li>hi<em>hi</em>​ : ground truth height</li><li>h^i<em>h</em>^<em>i</em>​ : predicted height</li></ul><p><strong>Classification loss:</strong> This measures how accurate the predicted probabilities are for each detected object. Here, we use a standard classification loss, such as log loss (crossentropy) [12] and denote it by Lcls<em>Lcls</em>​ :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*goKm9mTTk5kNlcDX0rCUCg.png" /></figure><p>Where:</p><ul><li><em>M</em> : total number of detected bounding boxes</li><li><em>C</em> : total number of classes</li><li>yi​ : ground truth label for detection i<em>i</em></li><li>y^i: predicted class label for detection i<em>i</em></li></ul><p>To define a final loss that measures the model’s overall performance, we combine the classification loss and regression loss weighted by a balancing parameter λ:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/143/1*yH9ycKXH0_H6AiB1YU8u6w.png" /></figure><h3>Evaluation</h3><p>During an interview, it is crucial to discuss how to evaluate an ML system. The interviewer usually wants to know which metrics you’d choose and why. This section describes how object detection systems are usually evaluated, and then selects important metrics for offline and online evaluations.</p><p>An object detection model usually needs to detect NN different objects in an image. To measure the overall performance of the model, we evaluate each object separately and then average the results.</p><p>Figure 3.8 shows the output of an object detection model. It shows both the ground truth and detected bounding boxes. As shown, the model detected 6 bounding boxes, while we only have two instances of the object.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*uCBNA8S6W__nf9jeR9KKww.png" /></figure><p>When is a predicted bounding box considered correct? To answer this question, we need to understand the definition of Intersection Over Union.</p><p><strong>Intersection Over Union (IOU):</strong> IOU measures the overlap between two bounding boxes. Figure 3.93.9 shows a visual representation of IOU.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*74MFH9b6JB5XfCdgQRTiTQ.png" /></figure><p>IOU determines whether a detected bounding box is correct. An IOU of 1 is ideal, indicating the detected bounding box and the ground truth bounding box are fully aligned. In practice, it’s rare to see an IOU of 1 . A higher IOU means the predicted bounding box is more accurate. An IOU threshold is usually used to determine whether a detected bounding box is correct (true positive) or incorrect (false positive). For example, an IOU threshold of 0.70.7 means any detection that has an overlap of 0.70.7 or higher with a ground truth bounding box, is a correct detection.</p><p>Now we know what IOU is and how to determine correct and incorrect bounding box predictions, let’s discuss metrics for offline evaluation.</p><h4>Offline metrics</h4><p>Model development is an iterative process. We use offline metrics to quickly evaluate the performance of newly developed models. Here are some metrics that might be useful for the object detection system:</p><ul><li>Precision</li><li>Average precision</li><li>Mean average precision</li></ul><p>Precision</p><p>This is the fraction of correct detections among all detections across all images. A high precision value shows the system’s detections are more reliable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/269/1*3n_0XaeZrjpYou4u1Zkg6A.png" /></figure><p>In order to calculate precision, we need to pick an IOU threshold. Let’s use an example to better understand this. Figure 3.103.10 shows a set of ground truth bounding boxes and detected bounding boxes, with their respective IOUs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*onDKx7lQxSxAPB1aA5idBA.png" /></figure><p>Let’s calculate precision for three different IOU thresholds: 0.7, 0.5, and 0.1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*PprC7-QTqQLLrb0aEkbD2w.png" /></figure><p>As you may have noticed, the primary disadvantage of this metric is that precision varies with different IOU thresholds. Therefore, it’s difficult to understand the model’s overall performance by looking at a precision score with a particular IOU threshold. Average precision addresses this limitation.</p><p><strong>Average Precision (AP)</strong> This metric computes precision across various IOU thresholds and calculates their average. The AP formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*HB9naqh3QMyeHqzZkVpWoA.png" /></figure><p>Where P(r)<em>P</em>(<em>r</em>) is the precision at IOU threshold r<em>r</em>.</p><p>The above formula can be approximated by a discrete summation over a predefined list of thresholds. For example, in the pascal VOC2008 benchmark [13], the AP is calculated across 11 evenly-spaced threshold values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/165/1*OqSTV4YXkpZqPm05Nulquw.png" /></figure><p>AP summarizes the model’s overall precision for a specific object class (e.g., human faces). To measure the model’s overall precision across all object classes (e.g., human faces and license plates), we need to use mean average precision.</p><p><strong>Mean average precision (mAP)</strong> This is the average of AP across all object classes. This metric summarizes the model’s overall performance. Here is the formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*_wZFf_oYWyQK9U24wAA0cQ.png" /></figure><p>Where C<em>C</em> is the total number of object classes the model detects.</p><p>The mAP metric is commonly used to evaluate object detection systems. To find out which thresholds are used in standard benchmarks, refer to [14] [15].</p><h4>Online metrics</h4><p>According to the requirements, the system needs to protect the privacy of individuals. One way to measure this is to count the number of user reports and complaints. We can also rely on human annotators to spot-check the percentage of incorrectly blurred images. Other metrics that measure bias and fairness are also critical. For example, we want to blur human faces equally well across different races and age groups. But measuring bias, as stated in the requirements, is out of the scope.</p><p>To conclude the evaluation section, we use mAP and AP as our offline metrics. mAP measures the overall precision of the model, while AP gives us insight into the precision of the model in particular classes. The main metric of the online evaluation is “user reports.”</p><h3>Serving</h3><p>In this section, we first talk about a common problem that may occur in object detection systems: overlapping bounding boxes. Next, we propose an overall ML system design.</p><h4>Overlapping bounding boxes</h4><p>When running an object detection algorithm on an image, it is very common to see bounding boxes overlap. This is because the RPN network proposes various highly overlapping bounding boxes around each object. It is important to narrow down these bounding boxes to a single bounding box per object during inference.</p><p>A widely used solution is an algorithm called “Non-maximum suppression” (NMS) [16]. Let’s examine how it works.</p><p>NMS</p><p>NMS is a post-processing algorithm designed to select the most appropriate bounding boxes. It keeps highly confident bounding boxes and removes overlapping bounding boxes. Figure 3.11 shows an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/420/1*NxpQPWiuAlZxwA7GWbk0PA.png" /></figure><p>NMS is a commonly asked algorithm in ML system design interviews, so you’re encouraged to have a good understanding of it [17].</p><h4>ML system design</h4><p>As illustrated in Figure 3.12, we propose an ML system design for the blurring system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*d8ZDR2Iai3IGsck8bhiW9w.png" /></figure><p>Let’s examine each pipeline in more detail.</p><p>Batch prediction pipeline</p><p>Based on the requirements gathered, latency is not a big concern because we can display existing images to users while new ones are being processed. Since instant results are not required, we can utilize batch prediction and precompute the object detection results.</p><p><strong>Preprocessing</strong> Raw images are preprocessed by this component. This section does not discuss the preprocess operations as we have already discussed them in the feature engineering section.</p><p><strong>Blurring service</strong> This performs the following operations on a Street View image:</p><ol><li>Provides a list of objects detected in the image.</li><li>Refines the list of detected objects using the NMS component.</li><li>Blurs detected objects.</li><li>Stores the blurred image in object storage (Blurred Street View images).</li></ol><p>Note that the preprocessing and blurring services are separate in the design. The reason is preprocessing images tends to be a CPU-bound process, whereas blurring service relies on GPU. Separating these services has two benefits:</p><ul><li>Scale the services independently based on the workload each receives.</li><li>Better utilization of CPU and GPU resources.</li></ul><p>Data pipeline</p><p>This pipeline is responsible for processing users’ reports, generating new training data, and preparing training data to be used by the model. Data pipeline components are mostly self-explanatory. Hard negative mining is the only component that needs more explanation.</p><p><strong>Hard negative mining.</strong> Hard negatives are examples that are explicitly created as negatives out of incorrectly predicted examples, and then added to the training dataset. When we retrain the model on the updated training dataset, it should perform better.</p><h3>Other Talking Points</h3><p>If time allows, here are some additional points to discuss:</p><ul><li>How Transformer-based object detection architectures differ from one-stage or twostage models, and what are their pros and cons [18].</li><li>Distributed training techniques to improve object detection on a larger dataset [19] [20].</li><li>How General Data Protection Regulation (GDPR) in Europe may affect our system [21].</li><li>Evaluate bias in face detection systems [22] [23].</li><li>How to continuously fine-tune the model [24].</li><li>How to use active learning [25] or human-in-the-loop ML [26] to select data points for training.</li></ul><h3>References</h3><ol><li>Google Street View. <a href="https://www.google.com/streetview">https://www.google.com/streetview</a>.</li><li>DETR. <a href="https://github.com/facebookresearch/detr">https://github.com/facebookresearch/detr</a>.</li><li>RCNN family. <a href="https://lilianweng.github.io/posts/2017-12-31-object-recognition-part-3">https://lilianweng.github.io/posts/2017-12-31-object-recognition-part-3</a>.</li><li>Fast R-CNN paper. <a href="https://arxiv.org/pdf/1504.08083.pdf">https://arxiv.org/pdf/1504.08083.pdf</a>.</li><li>Faster R-CNN paper. <a href="https://arxiv.org/pdf/1506.01497.pdf">https://arxiv.org/pdf/1506.01497.pdf</a>.</li><li>YOLO family. <a href="https://pyimagesearch.com/2022/04/04/introduction-to-the-yolo-family">https://pyimagesearch.com/2022/04/04/introduction-to-the-yolo-family</a>.</li><li>SSD. <a href="https://jonathan-hui.medium.com/ssd-object-detection-single-shot-multibox-detector-for-real-time-processing-9bd8deac0e06">https://jonathan-hui.medium.com/ssd-object-detection-single-shot-multibox-detector-for-real-time-processing-9bd8deac0e06</a>.</li><li>Data augmentation techniques. <a href="https://www.kaggle.com/getting-started/190280">https://www.kaggle.com/getting-started/190280</a>.</li><li>CNN. <a href="https://en.wikipedia.org/wiki/Convolutional_neural_network">https://en.wikipedia.org/wiki/Convolutional_neural_network</a>.</li><li>Forward pass and backward pass. <a href="https://www.youtube.com/watch?v=qzPQ8cEsVK8">https://www.youtube.com/watch?v=qzPQ8cEsVK8</a>.</li><li>MSE. <a href="https://en.wikipedia.org/wiki/Mean_squared_error">https://en.wikipedia.org/wiki/Mean_squared_error</a>.</li><li>Log loss. <a href="https://en.wikipedia.org/wiki/Cross_entropy">https://en.wikipedia.org/wiki/Cross_entropy</a>.</li><li>Pascal VOC. <a href="http://host.robots.ox.ac.uk/pascal/VOC/voc2008/index.html">http://host.robots.ox.ac.uk/pascal/VOC/voc2008/index.html</a>.</li><li>COCO dataset evaluation. <a href="https://cocodataset.org/#detection-eval">https://cocodataset.org/#detection-eval</a>.</li><li>Object detection evaluation. <a href="https://github.com/rafaelpadilla/Object-Detection-Metrics">https://github.com/rafaelpadilla/Object-Detection-Metrics</a>.</li><li>NMS. <a href="https://en.wikipedia.org/wiki/NMS">https://en.wikipedia.org/wiki/NMS</a>.</li><li>Pytorch implementation of NMS. <a href="https://learnopencv.com/non-maximum-suppression-theory-and-implementation-in-pytorch/">https://learnopencv.com/non-maximum-suppression-theory-and-implementation-in-pytorch/</a>.</li><li>Recent object detection models. <a href="https://viso.ai/deep-learning/object-detection/">https://viso.ai/deep-learning/object-detection/</a>.</li><li>Distributed training in Tensorflow. <a href="https://www.tensorflow.org/guide/distributed_training">https://www.tensorflow.org/guide/distributed_training</a>.</li><li>Distributed training in Pytorch. <a href="https://pytorch.org/tutorials/beginner/dist_overview.html">https://pytorch.org/tutorials/beginner/dist_overview.html</a>.</li><li>GDPR and ML. <a href="https://www.oreilly.com/radar/how-will-the-gdpr-impact-machine-learning">https://www.oreilly.com/radar/how-will-the-gdpr-impact-machine-learning</a>.</li><li>Bias and fairness in face detection. <a href="http://sibgrapi.sid.inpe.br/col/sid.inpe.br/sibgrapi/2021/09.04.19.00/doc/103.pdf">http://sibgrapi.sid.inpe.br/col/sid.inpe.br/sibgrapi/2021/09.04.19.00/doc/103.pdf</a>.</li><li>AI fairness. <a href="https://www.kaggle.com/code/alexisbcook/ai-fairness">https://www.kaggle.com/code/alexisbcook/ai-fairness</a>.</li><li>Continual learning. <a href="https://towardsdatascience.com/tag/fine-tuning/">https://towardsdatascience.com/tag/fine-tuning/</a>.</li><li>Active learning. <a href="https://en.wikipedia.org/wiki/Active_learning_(machine_learning)">https://en.wikipedia.org/wiki/Active_learning_(machine_learning)</a>.</li><li>Human-in-the-loop ML. <a href="https://arxiv.org/pdf/2108.00941.pdf">https://arxiv.org/pdf/2108.00941.pdf</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=98e376451490" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning System Design Interview — Visual Search System]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-visual-search-system-73a87068384e?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/73a87068384e</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Wed, 20 May 2026 19:26:00 GMT</pubDate>
            <atom:updated>2026-05-20T19:26:00.914Z</atom:updated>
            <content:encoded><![CDATA[<h3>Visual Search System</h3><p>A system helps users discover images that are visually similar to a selected image. In this chapter, we design a visual search system similar to Pinterest’s [1] [2].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/627/1*usEn911ETR5hPadP6jwZMg.png" /></figure><h3>Clarifying Requirements</h3><p>Here’s a typical interaction between a candidate and an interviewer.</p><p><strong>Candidate</strong>: Should we rank the results from most similar to least similar?<br><strong>Interviewer</strong>: Images that appear first in the list should be more similar to the query image.</p><p><strong>Candidate</strong>: Should the system support videos, too?<br><strong>Interviewer</strong>: Let’s focus only on images.</p><p><strong>Candidate</strong>: A platform like Pinterest allows users to select an image crop and retrieve similar images. Should we support that functionality?<br><strong>Interviewer</strong>: Yes.</p><p><strong>Candidate</strong>: Are the displayed images personalized to the user?<br><strong>Interviewer</strong>: For simplicity, let’s not focus on personalization. A query image yields the same results, regardless of who searches for it.</p><p><strong>Candidate</strong>: Can the model use the metadata of the query image, such as image tags?<br><strong>Interviewer</strong>: In practice, the model uses image metadata. But for simplicity, let’s assume we don’t rely on the metadata, but only on the image pixels.</p><p><strong>Candidate</strong>: Can users perform other actions such as save, share, or like? These actions can help label training data.<br><strong>Interviewer</strong>: Great point. For simplicity, let’s assume the only supported action is image clicks.</p><p><strong>Candidate</strong>: Should we moderate the images?<br><strong>Interviewer</strong>: It’s important to keep the platform safe, but content moderation is out of scope.</p><p><strong>Candidate</strong>: We can construct training data online and label them based on user interactions. Is this the expected way to construct training data?<br><strong>Interviewer</strong>: Yes, that sounds reasonable.</p><p><strong>Candidate</strong>: How fast should the search be? Assuming we have 100–200 billion images on the platform, the system should be able to retrieve similar images quickly. Is that a reasonable assumption?<br><strong>Interviewer</strong>: Yes, that is a reasonable assumption.</p><p>Let’s summarize the problem statement. We are asked to design a visual search system. The system retrieves images similar to the query image provided by the user, ranks them based on their similarities to the query image, and then displays them to the user. The platform only supports images, with no video or text queries allowed. For simplicity, no personalization is required.</p><h3>Frame the Problem as an ML Task</h3><p>In this section, we choose a well-defined ML objective and frame the visual search problem as an ML task.</p><h4>Defining the ML objective</h4><p>In order to solve this problem using an ML model, we need to create a well-defined ML objective. A potential ML objective is to accurately retrieve images that are visually similar to the image the user is searching for.</p><h4>Specifying the system’s input and output</h4><p>The input of a visual search system is a query image provided by the user. The system outputs images that are visually similar to the query image, and the output images are ranked by similarities. Figure 2.2 shows the input and output of a visual search system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*LGjLb9l25ahFYi_SjyEmMQ.png" /></figure><h4>Choosing the right ML category</h4><p>The output of the model is a set of ranked images that are similar to the query image. As a result, visual search systems can be framed as a ranking problem. In general, the goal of ranking problems is to rank a collection of items, such as images, websites, products, etc., based on their relevance to a query, so that more relevant items appear higher in the search results. Many ML applications, such as recommendation systems, search engines, document retrieval, and online advertising, can be framed as ranking problems. In this chapter, we will use a widely-used approach called representation learning. Let’s examine this in more detail.</p><p><strong>Representation learning.</strong> In representation learning [3], a model is trained to transform input data, such as images, into representations called embeddings. Another way of describing this is that the model maps input images to points in an N-dimensional space called embedding space. These embeddings are learned so that similar images have embeddings that are in close proximity to each other, in the space. Figure 2.3 illustrates how two similar images are mapped onto two points in close proximity within the embedding space. To demonstrate, we visualize image embeddings (denoted by ‘x<em>x</em>’) in a 22-dimensional space. In reality, this space is N<em>N</em>-dimensional, where N<em>N</em> is the size of the embedding vector.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*o4nNnxTyZ3gGyfWnvTv8Wg.png" /></figure><p>How to rank images using representation learning?</p><p>First, the input images are transformed into embedding vectors. Next, we calculate the similarity scores between the query image and other images on the platform by measuring their distances in the embedding space. The images are ranked by similarity scores, as shown in Figure 2.4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/474/1*JdPYSE19pQpzyh3fViSsLA.png" /></figure><p>At this point, you may have many questions, including how to ensure similar images are placed close to each other in the embedding space, how to define the similarity, and how to train such a model. We will talk more about these in the model development section.</p><h3>Data Preparation</h3><h4>Data engineering</h4><p>Aside from generic data engineering fundamentals, it’s important to understand what data is available. As a visual search system mainly focuses on users and images, we have the following data available:</p><ul><li>Images</li><li>Users</li><li>User-image interactions</li></ul><p>Images</p><p>Creators upload images, and the system stores the images and their metadata, such as owner id, contextual information (e.g., upload time), tags, etc. Table 2.1 shows a simplified example of image metadata.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/338/1*s9i1teqQxHXCBAVnIfBePQ.png" /></figure><p>Users</p><p>User data contains demographic attributes associated with users, such as age, gender, etc. Table 2.2 shows an example of user data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*Ocu6Te-3SIHMuTzwrVjyBw.png" /></figure><p>User-image interactions</p><p>Interaction data contains different types of user interactions. Based on the requirements gathered, the primary types of interactions are impressions and clicks. Table 2.3 shows an overview of interaction data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/781/1*zrmtSNGSar01Sj5_LpMvhQ.png" /></figure><h4>Feature engineering</h4><p>In this section, you are expected to talk about engineering great features and preparing them as model inputs. This usually depends on how we framed the task and what the model’s inputs are. In the earlier “Framing the problem as an ML task” section, we framed the visual search system as a ranking problem and used representation learning to solve it. In particular, we employed a model which expects an image as input. The image needs to be preprocessed before being passed to the model. Let’s take a look at common image preprocessing operations:</p><ul><li><strong>Resizing:</strong> Models usually require fixed image sizes (e.g., 224×224224×224)</li><li><strong>Scaling:</strong> Scale pixel values of the image to the range of 00 and 11</li><li><strong>Z-score normalization:</strong> Scale pixel values to have a mean of 00 and variance of 11</li><li><strong>Consistent :</strong> Ensuring images have a consistent color mode (e.g., RGB or CMYK)</li></ul><h3>Model Development</h3><h4>Model selection</h4><p>We choose neural networks because:</p><ul><li>Neural networks are good at handling unstructured data, such as images and text</li><li>Unlike many traditional machine learning models, neural networks are able to produce the embeddings we need for representation learning</li></ul><p>What types of neural network architectures should we use? It is essential that the architecture works with images. CNN-based architectures such as ResNet [4] or more recent Transformer-based architectures [5] such as ViT [6] perform well with image inputs. Figure 2.5 shows a simplified model architecture that transforms the input image into an embedding vector. The number of convolution layers, the number of neurons in fullyconnected layers, and the size of the embedding vector are hyperparameters typically chosen via experimentation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*a3mHH048pxha7g9nPAlodw.png" /></figure><h4>Model training</h4><p>In order to retrieve visually similar images, a model must learn representations (embeddings) during training. In this section, we discuss how to train a model to learn image representations.</p><p>A common technique for learning image representations is contrastive training [7]. With this technique, we train the model to distinguish similar and dissimilar images. As Figure 2.6 shows, we provide the model with a query image (left), one similar image to the query image (highlighted dog image on the right), and a few dissimilar images (also right.) During training, the model learns to produce representations in which the similar image more closely resembles the query image, than do other images on the right side of Figure 2.6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/363/1*q4Xx22BSMXn6FTjZXwgdMg.png" /></figure><p>To train the model using the contrastive training technique, we first need to construct training data.</p><h4>Constructing the dataset</h4><p>As described earlier, each data point used for training contains a query image, a positive image that’s similar to the query image, and n−1<em>n</em>−1 negative images that are dissimilar to the query image. The ground truth label of the data point is the index of the positive image. As Figure 2.7 shows, along with the query image (q), we have n<em>n</em> other images, of which one is similar to the q (image of the dog), and the other n−1<em>n</em>−1 images are dissimilar. The ground truth label for this data point is the index of the positive image, which is 22 (the second image among the n<em>n</em> images in Figure 2.7.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*vsS5CD-U0jiA491iLqvw2g.png" /></figure><p>To construct a training data point, we randomly choose a query image and n−1<em>n</em>−1 images as negative images. To select a positive image, we have the following three options:</p><ul><li>Use human judgment</li><li>Use interactions such as user clicks as a proxy for similarity</li><li>Artificially create a similar image from the query image, known as self-supervision</li></ul><p>Let’s evaluate each option.</p><p>Use human judgments</p><p>This approach relies on human contractors manually finding similar images. Human involvement produces accurate training data, but using human annotators is expensive and time-consuming.</p><p>Use interactions such as user click as a proxy for similarity</p><p>In this approach, we measure similarity based on interaction data. As an example, when a user clicks on an image, the clicked image is considered to be similar to the query image q.</p><p>This approach does not require manual work and can generate training data automatically. However, the click signal is usually very noisy. Users sometimes click on images even when the image is not similar to the query image. Additionally, this data is very sparse, and we may not have click data available for lots of the images. The use of noisy and sparse training data leads to poor performance.</p><p>Artificially create a similar image from the query image</p><p>In this approach, we artificially create a similar image from the query image. For example, we can augment the query image by rotating it and using the newly generated image as a similar image. Recently developed frameworks such as SimCLR [7] and MoCo [8] use the same approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*RoFnONTZTw6CtTBzd7QuOQ.png" /></figure><p>An advantage of this method is that no manual work is required. We can implement simple data augmentation logic to create similar images. In addition, the constructed training data is not noisy, since augmenting an image always results in a similar image. The major drawback of this approach is that the constructed training data differs from the real data. In practice, similar images are not augmented versions of the query image; they are visually and semantically similar, but are distinct.</p><p>Which approach works best in our case?</p><p>In an interview setting, it’s critical you propose various options and discuss their tradeoffs. There is usually not a single best solution that always works. Here, we use the self-supervision option for two reasons. Firstly, there is no upfront cost associated with it, since the process can be automated. Secondly, various frameworks such as SimCLR [7] have shown promising results when trained on a large dataset. Since we have access to billions of images on the platform, this approach might be a good fit.</p><p>We can always switch to other labeling methods if the experiment results are unsatisfactory. For example, we can start with the self-supervision option and later use click data for labeling. We can also combine the options. For example, we may use clicks to build our initial training data and rely on human annotators to identify and remove noisy data points. Discussing different options and trade-offs with the interviewer is critical to make good design decisions.</p><p>Once we construct the dataset, it’s time to train the model using a proper loss function.</p><h4>Choosing the loss function</h4><p>As Figure 2.9 shows, the model takes images as input and produces an embedding for each input image. Ex<em>Ex</em>​ denotes the embedding of the image x<em>x</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*E6jVg7-8Oc10t8ht7FS6Aw.png" /></figure><p>The goal of the training is to optimize the model parameters so that similar images have embeddings close to each other in the embedding space. As Figure 2.10 shows, the positive image and the query image should become closer during training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*FnluYF8-KvH3RqZUl0MNAg.png" /></figure><p>To achieve this goal, we need to use a loss function to measure the quality of the produced embeddings. Different loss functions are designed for contrastive training, and interviewers don’t usually expect you to hold an in-depth discussion. However, it is crucial to have a high-level understanding of how contrastive loss functions work.</p><p>We are going to briefly discuss how a simplified contrastive loss operates. If you are interested in learning more about contrastive losses, refer to [9].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*Htxul0wl_rGZ_3fBOv8JNg.png" /></figure><p>As Figure 2.11 shows, we compute the contrastive loss in three steps.</p><p><strong>Compute similarities.</strong> First, we compute the similarities between the query image and the embeddings of other images. Dot product [10] and cosine similarity [11] are widely used to measure the similarity between points in the embedding space. Euclidean distance [12] can also measure the similarity. However, Euclidean distance usually performs poorly in high dimensions because of the curse of dimensionality [13]. To learn more about the curse of dimensionality issues, read [14].</p><p><strong>Softmax.</strong> A softmax function is applied over the computed distances. This ensures the values sum up to one, which allows the values to be interpreted as probabilities.</p><p><strong>Cross-entropy.</strong> Cross-entropy [15] measures how close the predicted probabilities are to the ground truth labels. When the predicted probabilities are close to the ground truth, it shows that the embeddings are good enough to distinguish the positive image from the negative ones.</p><p>In the interview, you can also discuss the possibility of using a pre-trained model. For example, we could leverage a pre-trained contrastive model and fine-tune it using the training data. These pre-trained models have already been trained on large datasets, and therefore they have learned good image representations. This significantly reduces the training time compared to training a model from scratch.</p><h3>Evaluation</h3><p>After we develop the model, we can discuss the evaluation. In this section, we cover important metrics for offline and online evaluations.</p><h4>Offline metrics</h4><p>Based on the given requirements, an evaluation dataset is available for offline evaluation. Let’s assume each data point has a query image, a few candidate images, and a similarity score for each candidate image and the query image pair. A similarity score is an integer number between 00 to 55, where 00 indicates no similarity and 55 indicates two images are visually and semantically very similar. For each data point in the evaluation dataset, we compare the ranking produced by the model with the ideal ranking, based on the ground truth scores.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*5ELCACmhNXye8vpHrsXV0g.png" /></figure><p>Now, let’s examine offline metrics that are commonly used in search systems. Note that search, information retrieval, and recommendation systems usually share the same offline metrics.</p><ul><li>Mean reciprocal rank (MRR)</li><li>Recall@k</li><li>Precision@k</li><li>Mean average precision (mAP)</li><li>Normalized discounted cumulative gain (nDCG)</li></ul><p><strong>MRR.</strong> This metric measures the quality of the model by considering the rank of the first relevant item in each output list produced by the model, and then averaging them. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/162/1*JZN0cusA60_Fi80J2qaVwQ.png" /></figure><p>Where m<em>m</em> is the total number of output lists and ranki<em>ranki</em>​ refers to the rank of the first relevant item in the <em>i</em>th output list.</p><p>Figure 2.13 illustrates how this works. For each of the 4 ranked lists, we compute the reciprocal rank (RR) and then calculate the average value of the RRs to get the MRR.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/306/1*4JA5YRmJyTZkwlnGl5ZZ7Q.png" /></figure><p>Let’s examine the shortcoming of this metric. Since MRR considers only the first relevant item and ignores other relevant items in the list, it does not measure the precision and ranking quality of a ranked list. For example, Figure 2.14 shows the outputs of two different models. The output of model 1 has 3 relevant items, while the output of model 2 has 1 relevant item. However, the reciprocal rank of both models is 0 5. Given this shortcoming, we will not use this metric.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/271/1*WTAIBCvDaFtBKOYnzdYq8Q.png" /></figure><p><strong>Recall@k.</strong> This metric measures the ratio between the number of relevant items in the output list and the total number of relevant items available in the entire dataset. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*AUQqdmMJ-J4ZH2aADUxEwA.png" /></figure><p>Even though recall@k measures how many relevant items the model failed to include in the output list, this isn’t always a good metric. Let’s understand why not. In some systems, such as search engines, the total number of relevant items can be very high. This negatively affects the recall as the denominator is very large. For example, when the query image is an image of a dog, the database may contain millions of dog images. The goal is not to return every dog image but to retrieve a handful of the most similar dog images.</p><p>Given recall@k doesn’t measure the ranking quality of the model, we will not use it.</p><p><strong>Precision@k.</strong> This metric measures the proportion of relevant items among the top k items in the output list. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*QM8mIG-PRsSLBBZ0oqvCPA.png" /></figure><p>This metric measures how precise the output lists are, but it doesn’t consider the ranking quality. For example, in Figure 2.15, if we rank more relevant items higher in the list, the precision won’t change. This metric is not ideal for our use case, since we need to measure both the precision and ranking quality of the results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/291/1*bD9zfSDS0NVzabIm-_ZgMA.png" /></figure><p><strong>mAP.</strong> This metric first computes the average precision (AP) for each output list, and then averages AP values.</p><p>Let’s first understand what AP is. It takes a list of k<em>k</em> items, such as images, and averages the precision@k at different values of k<em>k</em>. AP is high if more relevant items are located at the top of the list. For a list of size k<em>k</em>, the AP formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*0iii5c_yI6tzQMmo75hvpw.png" /></figure><p>Let’s look at an example to better understand the metric. Figure 2.16 shows AP calculations for each of the 4 output lists produced by the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*ATLAQtxewAjpXmDPeju4aA.png" /></figure><p>Since we average precisions, the overall ranking quality of the list is considered. However, mAP is designed for binary relevances; in other words, it works well when each item is either relevant or irrelevant. For continuous relevance scores, nDCG is a better choice.</p><p><strong>nDCG.</strong> This metric measures the ranking quality of an output list and shows how good the ranking is, compared to the ideal ranking. First, let’s explain DCG and then discuss nDCG.</p><p>What is DCG?</p><p>DCG calculates the cumulative gain of items in a list by summing up the relevance score of each item. Then the score is accumulated from the top of the output list to the bottom, with the score of each result discounted at lower ranks. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*ogT-wDWBxO8yGD7PxDRTGw.png" /></figure><p>Where reli<em>reli</em>​ is the ground truth relevance score of the image ranked at location i<em>i</em>.</p><p>What is nDCG?</p><p>Because DCG sums up the relevance scores of items and discounts their positions, the result of DCG could be any value. In order to get a more meaningful score, we need to normalize DCG. For this, nDCG divides the DCG by the DCG of an ideal ranking. The formula is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/195/1*CB6mMFW8vqdEc04N_JJ2ug.png" /></figure><p>Where IDCGp<em>IDCGp</em>​ is the DCG<em>DCG</em> of the ideal ranking (a ranking ordered by the relevance scores of items). Note that in a perfect ranking system, the DCG is equal to IDCG.</p><p>Let’s use an example to better understand nDCG. In Figure 2.17, we can see a list of output images and their associated ground truth relevance scores produced by a search system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*OIAAES6350ypKX8UIUd3gg.png" /></figure><p>We can compute nDCG in 3 steps:</p><ol><li>Compute DCG</li><li>Compute IDCG</li><li>Divide DCG by IDCG</li></ol><p><strong>Compute DCG:</strong> The DCG for the current ranking produced by the model is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*oQ7N6e5ZESeOzCZpX_zOYw.png" /></figure><p><strong>Compute IDCG:</strong> The ideal ranking calculation is the same as the DCG calculation, except that it recommends the most relevant items first (Figure 2.18).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*yT2eFShYWmdvo1OFmJ-hsA.png" /></figure><p>The IDCG for the ideal ranking is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*Kvt_uQndvXT_gIkfCBO8xw.png" /></figure><p><strong>Divide DCG by IDCG:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/1*VqK7puGNTU6mWSnxRqfb3Q.png" /></figure><p>nDCG works well most times. Its primary shortcoming is that deriving ground truth relevance scores is not always possible. In our case, since the evaluation dataset contains similarity scores, we can use nDCG to measure the performance of the model during the offline evaluation.</p><h4>Online metrics</h4><p>In this section, we explore a few commonly used online metrics for measuring how quickly users can discover images they like.</p><p><strong>Click-through rate (CTR).</strong> This metric shows how often users click on the displayed items. CTR can be calculated using the following formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*48rCYb19EPlzWifn0ElfCg.png" /></figure><p>A high CTR indicates that users click on the displayed items often. CTR is commonly used as an online metric in search and recommendation systems, as we will see in later chapters.</p><p><strong>Average daily, weekly, and monthly time spent on the suggested images.</strong> This metric shows how engaged users are with the suggested images. When the search system is accurate, we expect this metric to increase.</p><h3>Serving</h3><p>At serving time, the system returns a ranked list of similar images based on a query image. Figure 2.19 shows the prediction pipeline and an indexing pipeline. Let’s look closer at each pipeline.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*YiHc7qU8my1J10Rwzz4MGA.png" /></figure><h4>Prediction pipeline</h4><p>Embedding generation service</p><p>This service computes the embedding of the input query image. As Figure 2.20 shows, it preprocesses the image and uses the trained model to determine the embedding.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*G4_QrHNT9L36LSGVh4K40Q.png" /></figure><p>Nearest neighbor service</p><p>Once we get the embedding of the query image, we need to retrieve similar images from the embedding space. The nearest neighbor service does this.</p><p>Let’s define the nearest neighbor search more formally. Given a query point “q” and a set of other points S, it finds the closest points to “q” in set S. Note that an image embedding is a point in N<em>N</em>-dimensional space, where N<em>N</em> is the size of the embedding vector. Figure 2.21 shows the top 3 nearest neighbors of image q. We denote the query image as q, and other images as x<em>x</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/689/1*Vsa-qOKn8h9nCRrG2LT9eA.png" /></figure><p>Re-ranking service</p><p>This service incorporates business-level logic and policies. For example, it filters inappropriate results, ensures we don’t include private images, removes duplicates or near-duplicate results, and enforces other similar logic before displaying the final results to the user.</p><h4>Indexing pipeline</h4><p>Indexing service</p><p>All images on the platform are indexed by this service to improve search performance.</p><p>Another responsibility of the indexing service is to keep the index table updated. For example, when a creator adds a new image to the platform, the service indexes the embedding of the new image to make it discoverable by the nearest neighbor search.</p><p>Indexing increases memory usage because we store the embeddings of the entire images in an index table. Various optimizations are available to reduce memory usages, such as vector quantization [16] and product quantization [17].</p><h4>Performance of nearest neighbor (NN) algorithms</h4><p>Nearest neighbor search is a core component of information retrieval, search, and recommendation systems. A slight improvement in its efficiency leads to significant overall performance improvement. Given how critical this component is, the interviewer may want you to deep dive into this topic.</p><p>NN algorithms can be divided into two categories: exact and approximate. Let’s examine each in more detail.</p><p>Exact nearest neighbor</p><p>Exact nearest neighbor, also called linear search, is the simplest form of NN. It works by searching the entire index table, calculating the distance of each point with the query point q, and retrieving the k<em>k</em> nearest points. The time complexity is O(N×D)<em>O</em>(<em>N</em>×<em>D</em>), where N<em>N</em> is the total number of points and D<em>D</em> is the point dimension.</p><p>In a large-scale system in which N<em>N</em> may easily run into the billions, the linear time complexity is too slow.</p><p>Approximate nearest neighbor (ANN)</p><p>In many applications, showing users similar enough items is sufficient, and there is no need to perform an exact nearest neighbor search.</p><p>In ANN algorithms, a particular data structure is used to reduce the time complexity of NN search to sublinear (e.g., O(D×logN)<em>O</em>(<em>D</em>×<em>logN</em>). They usually require up-front preprocessing or additional space.</p><p>ANN algorithms can be divided into the following three categories:</p><ul><li>Tree-based ANN</li><li>Locality-sensitive hashing (LSH)-based ANN</li><li>Clustering-based ANN</li></ul><p>There are various algorithms within each category, and interviewers typically do not expect you to know every detail. It’s adequate to have a high-level understanding of them. So, let’s briefly cover each category.</p><p>Tree-based ANN</p><p>Tree-based algorithms form a tree by splitting the space into multiple partitions. Then, they leverage the characteristics of the tree to perform a faster search.</p><p>We form the tree by iteratively adding new criteria to each node. For instance, one criterion for the root node can be: gender == male. This means any point with a female attribute belongs to the left sub-tree.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*oBXwSp9icZuOCn78S4rVwg.png" /></figure><p>In the tree, non-leaf nodes split the space into two partitions given the criterion. Leaf nodes indicate a particular region in space. Figure 2.23 shows an example of the space divided into 7 regions. The algorithm only searches the partition that the query point belongs to.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*_pqXoYDITIu-eO2xjTXkgQ.png" /></figure><p>Typical tree-based methods are R-trees [18], Kd-trees [19], and Annoy (Approximate Nearest Neighbor Oh Yeah) [20].</p><p>Locality sensitive hashing (LSH):</p><p>LSH uses particular hash functions to reduce the dimensions of points and group them into buckets. These hash functions map points in close proximity to each other into the same bucket. LSH searches only those points belonging to the same bucket as the query point q. You can learn more about LSH by reading [21].</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*m2un4othf7Pu3OHj8orphg.png" /></figure><p>Clustering-based ANN</p><p>These algorithms form clusters by grouping the points based on similarities. Once the clusters are formed, the algorithms search only the subset of points in the cluster to which the query point belongs.</p><p>Which algorithm should we use?</p><p>Results from the exact nearest neighbor method are guaranteed to be accurate. This makes it a good option when we have limited data points, or if it’s required to have the exact nearest neighbors. However, when there are a large number of points, it’s impractical to run the algorithm efficiently. In this case, ANN methods are commonly used. While they may not return the exact points, they are more efficient in finding the nearest points.</p><p>Given the amount of data available in today’s systems, the ANN method is a more pragmatic solution. In our visual search system, we use ANN to find similar image embeddings.</p><p>For an applied ML role, the interviewer may ask you to implement ANN. Two widelyused libraries are Faiss [22] (developed by Meta) and ScaNN [23] (developed by Google). Each supports the majority of methods we have described in this chapter. You are encouraged to familiarize yourself with at least one of these libraries to better understand the concepts and to gain the confidence with which to implement the nearest neighbor search in an ML coding interview.</p><h3>Other Talking Points</h3><p>If there is extra time at the end of the interview, you might be asked follow-up questions or challenged to discuss advanced topics, depending on various factors such as the interviewer’s preference, the candidate’s expertise, role requirements, etc. Some topics to prepare for, especially for senior roles, are listed below.</p><ul><li>Moderate content in the system by identifying and blocking inappropriate images [24].</li><li>Different biases present in the system, such as positional bias [25][26].</li><li>How to use image metadata such as tags to improve search results. This is covered in Chapter 3 Google Street View Blurring System.</li><li>Smart crop using object detection [27].</li><li>How to use graph neural networks to learn better representations [28].</li><li>Support the ability to search images by a textual query. We examine this in Chapter 4.</li><li>How to use active learning [29] or human-in-the-loop [30] ML to annotate data more efficiently.</li></ul><h3>References</h3><ol><li>Visual search at pinterest. <a href="https://arxiv.org/pdf/1505.07647.pdf">https://arxiv.org/pdf/1505.07647.pdf</a>.</li><li>Visual embeddings for search at Pinterest. <a href="https://medium.com/pinterest-engineering/unifying-visual-embeddings-for-visual-search-at-pinterest-74ea7ea103f0">https://medium.com/pinterest-engineering/unifying-visual-embeddings-for-visual-search-at-pinterest-74ea7ea103f0</a>.</li><li>Representation learning. <a href="https://en.wikipedia.org/wiki/Feature_learning">https://en.wikipedia.org/wiki/Feature_learning</a>.</li><li>ResNet paper. <a href="https://arxiv.org/pdf/1512.03385.pdf">https://arxiv.org/pdf/1512.03385.pdf</a>.</li><li>Transformer paper. <a href="https://arxiv.org/pdf/1706.03762.pdf">https://arxiv.org/pdf/1706.03762.pdf</a>.</li><li>Vision Transformer paper. <a href="https://arxiv.org/pdf/2010.11929.pdf">https://arxiv.org/pdf/2010.11929.pdf</a>.</li><li>SimCLR paper. <a href="https://arxiv.org/pdf/2002.05709.pdf">https://arxiv.org/pdf/2002.05709.pdf</a>.</li><li><a href="https://openaccess.thecvf.com/content_CVPR_2020/papers/He_Momentum_Contrast_for_Unsupervised_Visual_Representation_Learning_CVPR_2020_paper.pdf">MoCo paper</a>.</li><li>Contrastive representation learning methods. <a href="https://lilianweng.github.io/posts/2019-11-10-self-supervised/">https://lilianweng.github.io/posts/2019-11-10-self-supervised/</a>.</li><li>Dot product. <a href="https://en.wikipedia.org/wiki/Dot_product">https://en.wikipedia.org/wiki/Dot_product</a>.</li><li>Cosine similarity. <a href="https://en.wikipedia.org/wiki/Cosine_similarity">https://en.wikipedia.org/wiki/Cosine_similarity</a>.</li><li>Euclidean distance. <a href="https://en.wikipedia.org/wiki/Euclidean_distance">https://en.wikipedia.org/wiki/Euclidean_distance</a>.</li><li>Curse of dimensionality. <a href="https://en.wikipedia.org/wiki/Curse_of_dimensionality">https://en.wikipedia.org/wiki/Curse_of_dimensionality</a>.</li><li>Curse of dimensionality issues in ML. <a href="https://www.mygreatlearning.com/blog/understanding-curse-of-dimensionality/">https://www.mygreatlearning.com/blog/understanding-curse-of-dimensionality/</a>.</li><li>Cross-entropy loss. <a href="https://en.wikipedia.org/wiki/Cross_entropy">https://en.wikipedia.org/wiki/Cross_entropy</a>.</li><li>Vector quantization. <a href="http://ws.binghamton.edu/fowler/fowler%20personal%20page/EE523_files/Ch_10_1%20VQ%20Description%20(PPT).pdf">http://ws.binghamton.edu/fowler/fowler%20personal%20page/EE523_files/Ch_10_1%20VQ%20Description%20(PPT).pdf</a>.</li><li>Product quantization. <a href="https://towardsdatascience.com/product-quantization-for-similarity-search-2f1f67c5fddd">https://towardsdatascience.com/product-quantization-for-similarity-search-2f1f67c5fddd</a>.</li><li>R-Trees. <a href="https://en.wikipedia.org/wiki/R-tree">https://en.wikipedia.org/wiki/R-tree</a>.</li><li>KD-Tree. <a href="https://kanoki.org/2020/08/05/find-nearest-neighbor-using-kd-tree/">https://kanoki.org/2020/08/05/find-nearest-neighbor-using-kd-tree/</a>.</li><li>Annoy. <a href="https://towardsdatascience.com/comprehensive-guide-to-approximate-nearest-neighbors-algorithms-8b94f057d6b6">https://towardsdatascience.com/comprehensive-guide-to-approximate-nearest-neighbors-algorithms-8b94f057d6b6</a>.</li><li>Locality-sensitive hashing. <a href="https://web.stanford.edu/class/cs246/slides/03-lsh.pdf">https://web.stanford.edu/class/cs246/slides/03-lsh.pdf</a>.</li><li>Faiss library. <a href="https://github.com/facebookresearch/faiss/wiki">https://github.com/facebookresearch/faiss/wiki</a>.</li><li>ScaNN library. <a href="https://github.com/google-research/google-research/tree/master/scann">https://github.com/google-research/google-research/tree/master/scann</a>.</li><li>Content moderation with ML. <a href="https://appen.com/blog/content-moderation/">https://appen.com/blog/content-moderation/</a>.</li><li>Bias in AI and recommendation systems. <a href="https://www.searchenginejournal.com/biases-search-recommender-systems/339319/#close">https://www.searchenginejournal.com/biases-search-recommender-systems/339319/#close</a>.</li><li>Positional bias. <a href="https://eugeneyan.com/writing/position-bias/">https://eugeneyan.com/writing/position-bias/</a>.</li><li>Smart crop. <a href="https://blog.twitter.com/engineering/en_us/topics/infrastructure/2018/Smart-Auto-Cropping-of-Images">https://blog.twitter.com/engineering/en_us/topics/infrastructure/2018/Smart-Auto-Cropping-of-Images</a>.</li><li>Better search with gnns. <a href="https://arxiv.org/pdf/2010.01666.pdf">https://arxiv.org/pdf/2010.01666.pdf</a>.</li><li>Active learning. <a href="https://en.wikipedia.org/wiki/Active_learning_(machine_learning)">https://en.wikipedia.org/wiki/Active_learning_(machine_learning)</a>.</li><li>Human-in-the-loop ML. <a href="https://arxiv.org/pdf/2108.00941.pdf">https://arxiv.org/pdf/2108.00941.pdf</a>.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73a87068384e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Machine Learning System Design Interview]]></title>
            <link>https://medium.com/@dibasbehera/machine-learning-system-design-interview-9d72881022c2?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d72881022c2</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Tue, 19 May 2026 19:11:00 GMT</pubDate>
            <atom:updated>2026-05-19T19:11:00.988Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction and Overview</h3><p>We have written this book because we want to help machine learning (ML) engineers and data scientists to succeed at ML system design interviews. This book can also be helpful for those who want to achieve a high-level conception of how ML is applied in the real world.</p><p>Many engineers think of ML algorithms such as logistic regression or neural networks as the entirety of an ML system. However, an ML system in production involves much more than just model development. ML systems are typically complex, consisting of a number of components, including data stacks to manage data, serving infrastructure to make the system available to millions of users, an evaluation pipeline to measure the performance of the proposed system, and monitoring to ensure the model’s performance doesn’t degrade over time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*l_S0Ogh2YTDnmoTYLhZpuw.png" /></figure><p>At an ML system design interview, you are expected to answer open-ended questions. For example, you might be asked to design a movie recommendation system or a video search engine. There is no single correct answer. The interviewer wants to evaluate your thought process, your in-depth understanding of various ML topics, your ability to design an end-to-end system, and your design choices based on the trade-offs of various options.</p><p>To succeed in designing complex ML systems, it is very important to follow a framework. Unstructured answers make the flow difficult to follow. In this introduction, we propose a framework that is used throughout this book to tackle questions about ML system design. The framework consists of the following key steps:</p><ol><li>Clarifying requirements</li><li>Framing the problem as an ML task</li><li>Data preparation</li><li>Model development</li><li>Evaluation</li><li>Deployment and serving</li><li>Monitoring and infrastructure</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*Sram7n2cMcKfOY3FRThnIg.png" /></figure><p>Every ML system design interview is different because the problem is open-ended, and there’s no one-size-fits-all way to succeed. The framework is intended to help you structure your thoughts, but there’s no need to follow it strictly. Be flexible. If an interviewer is mainly interested in model development, you should almost always adhere to their focus.</p><p>Let’s start by going over each step in the framework.</p><h3>Clarifying Requirements</h3><p>ML system design questions are usually intentionally vague, with the bare minimum of information. For example, an interview question could be: “design an event recommendation system”. The first step is to ask clarifying questions. But what kind of questions to ask? Well, we should ask questions to understand the exact requirements. Here is a list of categorized questions to help us get started:</p><ul><li><strong>Business objective</strong>. If we are asked to create a system to recommend vacation rentals, two possible motivations are to increase the number of bookings and increase the revenue.</li><li><strong>Features the system needs to support</strong>. What are some of the features that the system is expected to support which could affect our ML system design? For example, let’s assume we’re asked to design a video recommendation system. We might want to know if users can “like” or “dislike” recommended videos, as those interactions could be used to label training data.</li><li><strong>Data</strong>. What are the data sources? How large is the dataset? Is the data labeled?</li><li><strong>Constraints</strong>. How much computing power is available? Is it a cloud-based system, or should the system work on a device? Is the model expected to improve automatically over time?</li><li><strong>Scale of the system</strong>. How many users do we have? How many items, such as videos, are we dealing with? What’s the rate of growth of these metrics?</li><li><strong>Performance</strong>. How fast must prediction be? Is a real-time solution expected? Does accuracy have more priority or latency?</li></ul><p>This list is not exhaustive, but you can use it as a starting point. Remember that other topics, such as privacy and ethics, can also be important. At the end of this step, we’re expected to reach an agreement with the interviewer about the scope and requirements of the system. It’s generally a good idea to write down the list of requirements and constraints we gather. By doing so, we ensure everyone is on the same page.</p><h3>Frame the Problem as an ML Task</h3><p>Effective problem framing plays a critical role in solving ML problems. Suppose an interviewer asks you to increase the user engagement of a video streaming platform. Lack of user engagement is certainly a problem, but it’s not an ML task. So, we should frame it as an ML task in order to solve it. In reality, we should first determine whether or not ML is necessary for solving a given problem. In an ML system design interview, it’s safe to assume that ML is helpful. So, we can frame the problem as an ML task by doing the following:</p><ul><li>Defining the ML objective</li><li>Specifying the system’s input and output</li><li>Choosing the right ML category</li></ul><h4>Defining the ML objective</h4><p>A business objective can be to increase sales by 20% or to improve user retention. But the objective may not be well defined, and we cannot train a model simply by telling it, “increase sales by 20%”. For an ML system to solve a task, we need to translate the business objective into a well-defined ML objective. A good ML objective is one that ML models can solve. Let’s look at some examples as shown in Table 1.1. In later chapters, we will see more examples.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*kGyeZaiomR-33Gtzc-H73g.png" /></figure><h4>Specifying the system’s input and output</h4><p>Once we decide on the ML objective, we need to define the system’s inputs and outputs. For example, for a harmful content detection system on a social media platform, the input is a post, and the output is whether this post is considered harmful or not.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*xWox01aBKob03KzM4vJkpA.png" /></figure><p>In some cases, the system may consist of more than one ML model. If so, we need to specify the input and output of each ML model. For example, for harmful content detection, we may want to use one model to predict violence and another model to predict nudity. The system depends on these two models to determine if a post is harmful or not.</p><p>Another important consideration is that there might be multiple ways to specify each model’s input-output. Figure 1.4 shows an example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*LSAMSWSarF-sNBCBf6MqcA.png" /></figure><h4>Choosing the right ML category</h4><p>There are many different ways to frame a problem as an ML task. Most problems can be framed as one of the ML categories (leaf nodes) shown in Figure 1.5. As most readers are likely already familiar with them, we only provide an outline here.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*Be4tnT1UNXWkHI1oImGs2Q.png" /></figure><p><strong>Supervised learning.</strong> A supervised learning model learns a task by using a training dataset. In reality, many problems fall into this category, since learning from a labeled dataset usually leads to better results.</p><p><strong>Unsupervised learning.</strong> Unsupervised learning models make predictions by processing data that contain no correct answers. The goal of an unsupervised learning model</p><p><strong>Reinforcement learning.</strong> In reinforcement learning, a computer agent learns to perform a task through repeated trial-and-error interactions with the environment. For example, robots can be trained to walk around a room using reinforcement learning, and software programs like AlphaGo can compete in the game of Go by using reinforcement learning.</p><p>Compared to supervised learning, unsupervised learning and reinforcement learning are less popular in real-world systems, as ML models usually learn a specific task better when training data is available. As a result, the majority of problems we tackle in this book rely on supervised learning. Let’s take a closer look at the different categories of supervised learning.</p><p><strong>Classification model.</strong> Classification is the task of predicting a discrete class label; for example, whether an input image should be classified as “dog”, “cat”, or “rabbit”. Classification models can be divided into two groups:</p><ul><li><strong>Binary classification</strong> models predict a binary outcome. For example, the model predicts whether an image contains a dog or not</li><li><strong>Multiclass classification</strong> models classify the input into more than one class. For example, we can classify an image as a dog, cat, or rabbit</li></ul><p>In this step, you are expected to choose the correct ML category. Later chapters provide examples of how to choose the right category during an interview.</p><h4>Talking points</h4><p>Here are some topics we might want to talk about during an interview:</p><ul><li>What is a good ML objective? How do different ML objectives compare? What are the pros and cons?</li><li>What are the inputs and outputs of the system, given the ML objective?</li><li>If more than one model is involved in the ML system, what are the inputs and outputs of each model?</li><li>Does the task need to be learned in a supervised or unsupervised way?</li><li>Is it better to solve the problem using a regression or classification model? In the case of classification, is it binary or multiclass? In the case of regression, what is the output range?</li></ul><h3>Data Preparation</h3><p>ML models learn directly from data, meaning that data with predictive power is essential for training an ML model. This section aims to prepare high-quality inputs for ML models via two essential processes: data engineering and feature engineering. We will cover the important aspects of each process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/503/1*45u6uJTgii2AF-Leziv6qw.png" /></figure><h4>Data engineering</h4><p>Data engineering is the practice of designing and building pipelines for collecting, storing, retrieving, and processing data. Let’s briefly review data engineering fundamentals to understand the core components we may need.</p><h4>Data sources</h4><p>An ML system can work with data from many different sources. Knowing the data sources is a good way to answer many context questions, which may include: who collected it? How clean is the data? Can the data source be trusted? Is the data user-generated or system generated?</p><h4>Data storage</h4><p>Data storage, also called a database, is a repository for persistently storing and managing collections of data. Different databases are built to satisfy different use cases, so it’s important to understand at a high level how different databases work. You are usually not expected to know database internals during ML system design interviews.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/464/1*QZfAEuSlENRfA3-gkg9i5w.png" /></figure><p>Extract, transform, and load (ETL)</p><p>ETL consists of three phases:</p><ul><li><strong>Extract.</strong> This process extracts data from different data sources.</li><li><strong>Transform.</strong> In this phase, data is often cleansed, mapped, and transformed into a specific format to meet operational needs.</li><li><strong>Load.</strong> The transformed data is loaded into the target destination, which can be a file, a database, or a data warehouse [1].</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*2u5Pem5Hpq1dbGP7e8ZxJQ.png" /></figure><p>Data types</p><p>In ML, data types differ from those in programming languages, such as int, float, string, etc. At the high level, data types can be broken down into two types: structured and unstructured data, as shown in Figure 1.9.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*Z4GWDzWBn1FHuRKZNLoQEw.png" /></figure><p>Structured data follows a predefined data schema, whereas unstructured data does not. For example, dates, names, addresses, credit card numbers, and anything that can be represented in a tabular format with rows and columns, can be considered structured data. Unstructured data refers to data with no underlying data schema, such as images, audio files, videos, and text. Table 1.2 summarizes the key differences between structured and unstructured data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*1LCoXZxeI1CUugQtwDg38g.png" /></figure><p>As shown in Figure 1.10, ML models perform differently based on the type of data. Understanding and clarifying whether the data is structured or unstructured helps us choose the appropriate ML model during the model development step.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*g9txjooD1fmmcE6AgFEsyA.png" /></figure><p>Numerical data</p><p>Numerical data are any data points represented by numbers. As shown in Figure 1.9, numerical data is divided into continuous numerical data and discrete numerical data. For example, house prices can be considered as a continuous numerical value since a house price can take any value within a range. In contrast, the number of houses sold in the past year can be considered discrete numerical data, since it takes distinct values only.</p><p>Categorical data</p><p>Categorical data refers to data that can be stored and identified based on their assigned names or labels. For example, gender is categorical data since its value is from a limited set of age ranges. Categorical data can be divided into two groups: nominal and ordinal.</p><p>Nominal data refers to data with no numerical relationship between its categories. For example, gender is nominal data since there is no relationship between “male” and “female”. Ordinal data refers to data with a predetermined or sequential order. For example, rating data which takes three unique values from “not happy”, “neutral”, and “happy” is an example of ordinal data.</p><h4>Feature Engineering</h4><p>Feature engineering contains two processes:</p><ul><li>Using domain knowledge to select and extract predictive features from raw data</li><li>Transforming predictive features into a format usable by the model</li></ul><p>Choosing the appropriate features is one of the most important decisions when developing and training ML models. It’s essential to choose features that bring the most value, and this feature engineering process requires subject matter expertise and is also highly dependent upon the task at hand. To help you master this process, we give many examples throughout this book.</p><p>Once the predictive features are chosen, they need to be transformed into suitable formats using feature engineering operations, which we examine next.</p><h4>Feature engineering operations</h4><p>It’s quite common for some of the selected features to not be in a format the model can use. Feature engineering operations transform the selected features into a format the model can use. Techniques include handling missing values, scaling values that have skewed distributions, and encoding categorical features. The following list is not comprehensive, but it contains some of the most common operations for structured data.</p><p>Handling missing values</p><p>Data in production often has missing values, which can generally be addressed in two ways: deletion or imputation.</p><p><strong>Deletion.</strong> This method removes any records with a missing value in any of the features. Deletion can be divided into row deletion and column deletion. In column deletion, we remove the whole column representing a feature, if the feature has too many missing values. In row deletion, we remove a row representing a data point if the data point has many missing values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*DU8Vu0WJaTGD57UuXLL_dA.png" /></figure><p>The drawback of deletion is that it reduces the quantity of data the model can potentially use for training. This is not ideal since ML models tend to work better when they are exposed to more data.</p><p><strong>Imputation.</strong> Alternatively, we can impute the missing values by filling them with certain values. Some common practices include:</p><ul><li>Filling in missing values with their defaults</li><li>Filling in missing values with the mean, median, or mode (the most common value)</li></ul><p>The drawback of imputation is it may introduce noise to the data. It’s important to note that no technique is perfect for handling missing values, as each has its trade-offs</p><p>Feature scaling</p><p>Feature scaling refers to the process of scaling features to have a standard range and distribution. Let’s first look at why feature scaling might be needed.</p><p>Many ML models struggle to learn a task when the features of the dataset are in different ranges. For example, features such as age and income might have different value ranges. In addition, some models may struggle to learn the task when a feature has a skewed distribution. What are some of the feature scaling techniques? Let’s take a look.</p><p><strong>Normalization (min-max scaling).</strong> In this approach, the features are scaled, so all values are within the range [0,1][0,1] using the following formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/126/1*O5HMdYdTWeCXj2D6Gor18w.png" /></figure><p>Note that normalization does not change the distribution of the feature. In order to change the distribution of a feature to follow a standard distribution, standardization is used.</p><p><strong>Standardization (Z-score normalization).</strong> Standardization is the process of changing the distribution of a feature to have a 00 mean and a standard deviation of 11. The following formula is used to standardize a feature:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/121/1*vFwTUl28ECbccD879sIAgQ.png" /></figure><p>Where μ<em>μ</em> is the feature’s mean and σ<em>σ</em> is the standard deviation.</p><p><strong>Log scaling.</strong> To mitigate the skewness of a feature, a common technique called log scaling can be used, with the following formula:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/95/1*5LIRoDMSCO9qzsspNYMEhw.png" /></figure><p>Log transformation can make data distribution less skewed, and enable the optimization algorithm to converge faster.</p><p>Discretization (Bucketing)</p><p>Discretization is the process of converting a continuous feature into a categorical feature. For example, instead of representing height as a continuous feature, we can divide heights into discrete buckets and represent each height by the bucket to which it belongs. This allows the model to focus on learning only a few categories instead of attempting to learn an infinite number of possibilities.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/349/1*qBSD5iOMvNwSVyCTNSXVRA.png" /></figure><p>Discretization can also be applied to discrete features. For example, a user’s age is a discrete feature, but discretizing it reduces the number of categories, as shown in Table 1.3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*tcn6bpeUJvhW98IwVBQPBg.png" /></figure><p>Encoding categorical features</p><p>In most ML models, all inputs and outputs must be numerical. This means if a feature is categorical, we should encode it into numbers before sending it to the model. There are three common methods for converting categorical features into numeric representations: integer encoding, one-hot encoding, and embedding learning.</p><p><strong>Integer encoding.</strong> An integer value is assigned to each unique category value. For example, “Excellent” is 11, “Good” is 22, and “Bad” is 33. This method is useful if the integer values have a natural relationship with each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*vgoiYV2std8yD9ea4silag.png" /></figure><p>However, when there is no ordinal relationship between categorical features, integer encoding is not a good choice. One-hot encoding, which we will examine next, addresses this issue.</p><p><strong>One-hot encoding.</strong> With this technique, a new binary feature is created for each unique value. As shown in Figure 1.14, we replace the original feature (color) with three new binary features (red, green, and blue). For example, if a data point has a “red” color, we replace it with “11, 00, 00”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*gq8hhFQgK7U45z576swSgw.png" /></figure><p><strong>Embedding learning.</strong> Another way to encode a categorical feature is to use embedding learning. An embedding is a mapping of a categorical feature into an N<em>N</em>-dimensional vector. Embedding learning is the process of learning an N-dimensional vector for each unique value that the categorical feature may take. This approach is useful when the number of unique values the feature takes is very large. In this case, one-hot encoding is not a good option because it leads to very large vector sizes. We will see more examples in later chapters.</p><h4>Talking points</h4><p>Here are some topics we might want to discuss during the interview:</p><ul><li><strong>Data availability and data collection:</strong> What are the data sources? What data is available to us, and how do we collect it? How large is the data size? How often do new data come in?</li><li><strong>Data storage:</strong> Where is the data currently stored? Is it on the cloud or on user devices? Which data format is appropriate for storing the data? How do we store multimodal data, e.g., a data point that might contain both images and texts?</li><li><strong>Feature engineering:</strong> How do we process raw data into a form that’s useful for the models? What should we do about missing data? Is feature engineering required for this task? Which operations do we use to transform the raw data into a format usable by the ML model? Do we need to normalize the features? Which features should we construct from the raw data? How do we plan to combine data of different types, such as texts, numbers, and images?</li><li><strong>Privacy:</strong> How sensitive are the available data? Are users concerned about the privacy of their data? Is anonymization of user data necessary? Is it possible to store users’ data on our servers, or is it only possible to access their data on their devices?</li><li><strong>Biases:</strong> Are there any biases in the data? If yes, what kinds of biases are present, and how do we correct them?</li></ul><h3>Model Development</h3><p>Model development refers to the process of selecting an appropriate ML model and training it to solve the task at hand.</p><h4>Model selection</h4><p>Model selection is the process of choosing the best ML algorithm and architecture for a predictive modeling problem. In practice, a typical process for selecting a model is to:</p><ul><li><strong>Establish a simple baseline.</strong> For example, in a video recommendation system, the baseline can be obtained by recommending the most popular videos.</li><li><strong>Experiment with simple models.</strong> After we have a baseline, a good practice is to explore ML algorithms that are quick to train, such as logistic regression.</li><li><strong>Switch to more complex models.</strong> If simple models cannot deliver satisfactory results, we can then consider more complex models, such as deep neural networks.</li><li><strong>Use an ensemble of models if we want more accurate predictions.</strong> Using an ensemble of multiple models instead of only one may improve the quality of predictions. Creating an ensemble can be accomplished in three ways: bagging [2], boosting [3], and stacking [4], which will be discussed in later chapters.</li></ul><p>In an interview setting, it’s important to explore various model options and discuss their pros and cons. Some typical model options include:</p><ul><li>Logistic regression</li><li>Linear regression</li><li>Decision trees</li><li>Gradient boosted decision trees and random forests</li><li>Support vector machines</li><li>Naive Bayes</li><li>Factorization Machines (FM)</li><li>Neural networks</li></ul><p>When examining different options, it’s good to briefly explain the algorithm and discuss the trade-offs. For example, logistic regression may be a good option for learning a linear task, but if the task is complex, we may need to choose a different model. When choosing an ML algorithm, it’s important to consider different aspects of a model. For example:</p><ul><li>The amount of data the model needs to train on</li><li>Training speed</li><li>Hyperparameters to choose and hyperparameter tuning techniques</li><li>Possibility of continual learning</li><li>Compute requirements. A more complex model might deliver higher accuracy, but might require more computing power, such as a GPU instead of a CPU</li><li>Model’s interpretability [5]. A more complex model can give better performance, but its results may be less interpretable</li></ul><p>There is no single best algorithm that solves all problems. The interviewer wants to see if you have a good understanding of different ML algorithms, their pros and cons, and your ability to choose a model based on requirements and constraints. To help you improve model selection, this book contains a variety of model selection examples. This book assumes you are familiar with common ML algorithms. To refresh your memory, read [6].</p><h4>Model training</h4><p>Once the model selection is complete, it’s time to train the model. During this step, there are various topics you may want to discuss at an interview, such as:</p><ul><li>Constructing the dataset</li><li>Choosing the loss function</li><li>Training from scratch vs. fine-tuning</li><li>Distributed training</li></ul><p>Let’s look at each one.</p><p>Constructing the dataset</p><p>At an interview, it’s usually a good idea to talk about constructing the dataset for model training and evaluation. As Figure 1.15 illustrates, there are 5 steps in constructing the dataset.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*Sq5gXOVVy0xYFDAcbDp9PQ.png" /></figure><p>All the steps except “identify features and labels” are generic operations, which can be applied to any ML system design task. In this chapter, we will take a close look at each step, but in later chapters, we will mainly focus on “identify features and labels,” which is task-specific.</p><p>Collect the raw data</p><p>This is extensively discussed in the data preparation step, so we do not repeat it here.</p><p>Identify features and labels</p><p>During the feature engineering step, we have already discussed which features to use. So, let’s focus on creating labels for the data. There are two common ways to get labels: hand labeling and natural labeling.</p><p><strong>Hand labeling.</strong> This means individual annotators label the data by hand. For example, an individual annotator labels whether a post contains misinformation or not. Hand labeling leads to accurate labels since a human is involved in the process. However, hand labeling has many drawbacks; it is expensive and slow, introduces bias, requires domain knowledge, and is a threat to data privacy.</p><p><strong>Natural labeling.</strong> In natural labeling, the ground truth labels are inferred automatically without human annotations. Let’s see an example to better understand natural labels.</p><p>Suppose we want to design an ML system that ranks news feeds based on relevance. One possible way to solve this task is to train a model which takes a user and a post as input, and outputs the probability that the user will press the ``like” button after seeing this post. In this case, the training data are pairs of ⟨⟨ user, post ⟩⟩ and their corresponding label, which is 1 if the user liked the post, and 0 if they did not. This way, we are able to naturally label the training data without relying on human annotations.</p><p>In this step, it is important to clearly communicate how we obtain training labels and what the training data looks like.</p><p>Select a sampling strategy</p><p>It is often impractical to collect all the data, so sampling is an efficient way to reduce the amount of data in the system. Common sampling strategies include convenience sampling, snowball sampling, stratified sampling, reservoir sampling, and importance sampling. To learn more about sampling methods, you can refer to [7].</p><p>Split the data</p><p>Data splitting refers to the process of dividing the dataset into training, evaluation (validation), and test dataset. To learn more about data splitting techniques, refer to [8].</p><p>Address any class imbalances</p><p>A dataset with skewed class labels is called an imbalanced dataset. The class that makes up a larger proportion of the dataset is called the majority class, and the class which comprises a smaller proportion of the dataset is known as the minority class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*3v6-a6pT7t39-YcLXVdO3A.png" /></figure><p>An imbalanced dataset is a serious issue in model training, as it means a model may not have enough data to learn the minority class. There are different techniques to mitigate this issue. Let’s take a look at two commonly used approaches: resampling training data and altering the loss function.</p><p>Resampling training data</p><p>Resampling refers to the process of adjusting the ratio between different classes, making the data more balanced. For example, we can oversample the minority class (Figure 1.17) or undersample the majority class (Figure 1.18)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*zunaxcbolfaF7ZTrbf3gDA.png" /></figure><p>Altering the loss function</p><p>This technique alters the loss function to make it more robust against class imbalance. The high-level idea is to give more weight to data points from the minority class. A higher weight in the loss function penalizes the model more when it makes a wrong prediction about a minority class. This forces the model to learn minority classes more effectively. Two commonly used loss functions to mitigate class imbalance are class-balanced loss [9] and focal loss [10][11].</p><p>Choosing the loss function</p><p>Once the dataset is constructed, we need to choose a proper loss function to train the model. A loss function is a measurement of how accurate the model is at predicting an expected outcome. The loss function allows the optimization algorithm to update the model’s parameters during the training process in order to minimize the loss.</p><p>Designing a novel loss function is not easy. In ML interviews, you are usually expected to select a loss function from a list of existing loss functions based on how you framed the problem. Sometimes, you might need to make minor changes to the loss function to make it specific to the problem. We will provide more examples in later chapters.</p><p>Training from scratch vs. fine-tuning</p><p>One topic that might be useful to discuss briefly is training from scratch vs. fine-tuning. Fine-tuning means continuing to train the model on new data by making small changes to its learned parameters. This is a design decision you may need to discuss with the interviewer.</p><p>Distributed training</p><p>Training at scale becomes increasingly important because models grow bigger over time, and the size of the dataset also increases dramatically. Distributed training is commonly used to train the model, by dividing the work among multiple worker nodes. These worker nodes operate in parallel in order to speed up model training. There are two main types of distributed training: data parallelism [12] and model parallelism [13].</p><p>Depending on which task we are solving, employing distributed training may be necessary. In such cases, it’s important to discuss this topic with the interviewer. Note that distributed training is a generic topic you can talk about irrespective of the specific task.</p><h4>Talking points</h4><p>Some talking points are listed below:</p><ul><li><strong>Model selection:</strong> Which ML models are suitable for the task, and what are their pros and cons. Here’s a list of topics to consider during model selection:</li><li>The time it takes to train</li><li>The amount of training data the model expects</li><li>The computing resources the model may need</li><li>Latency of the model at inference time</li><li>Can the model be deployed on a user’s device?</li><li>Model’s interpretability. Making a model more complex may increase its performance, but the results might be harder to interpret</li><li>Can we leverage continual training, or should we train from scratch?</li><li>How many parameters does the model have? How much memory is needed?</li><li>For neural networks, you might want to discuss typical architectures/blocks, such as ResNet or Transformer-based architectures. You can also discuss the choice of hyperparameters, such as the number of hidden layers, the number of neurons, activation functions, etc.</li><li><strong>Dataset labels:</strong> How should we obtain the labels? Is the data annotated, and if so, how good are the annotations? If natural labels are available, how do we get them? How do we receive user feedback on the system? How long does it take to get natural labels?</li><li><strong>Model training.</strong></li><li>What loss function should we choose? (e.g., Cross-entropy [14], MSE [15], MAE [16], Huber loss [17], etc.)</li><li>What regularization should we use? (e.g., L1 [19], L2 [18], Entropy Regularization [19], K-fold CV [20], or dropout [21])</li><li>What is backpropagation?</li><li>You may need to describe common optimization methods such as SGD [22], AdaGrad [23], Momentum [24], and RMSProp [25].</li><li>What activation functions do we want to use and why? (e.g., ELU [26], ReLU [27], Tanh [28], Sigmoid [29]).</li><li>How to handle an imbalanced dataset?</li><li>What is the bias/variance trade-off?</li><li>What are the possible causes of overfitting and underfitting? How to address them?</li><li><strong>Continual learning:</strong> Do we want to train the model online with each new data point? Do we need to personalize the model to each user? How often do we retrain the model? Some models need to be retrained daily or weekly, and others monthly or yearly.</li></ul><h3>Evaluation</h3><p>The next step after the model development is evaluation, which is the process of using different metrics to understand an ML model’s performance. In this section, we examine two evaluation methods, offline and online.</p><h4>Offline evaluation</h4><p>Offline evaluation refers to evaluating the performance of ML models during the model development phase. To evaluate a model, we usually first make predictions using the evaluation dataset. Next, we use various offline metrics to measure how close the predictions are to the ground truth values. Table 1.4 shows some of the commonly used metrics for different tasks.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/1*GGa7xuDFS6AfGUuvxOYY7Q.png" /></figure><p>During an interview, it’s important to identify suitable metrics for offline evaluation. This depends on the task at hand and how we have framed it. For example, if we try to solve a ranking problem, we might need to discuss ranking metrics and their trade-offs.</p><h4>Online evaluation</h4><p>Online evaluation refers to the process of evaluating how the model performs in production after deployment. To measure the impact of the model, we need to define different metrics. Online metrics refer to those we use during an online evaluation and are usually tied to business objectives. Table 1.5 shows various metrics for different problems.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*aE23DgbYC2DmdvSzqN_tig.png" /></figure><p>In practice, companies usually track many online metrics. At an interview, we need to select some of the most important ones to measure the impact of the system. As opposed to offline metrics, choosing online metrics Online metrics is subjective and depends upon product owners and business stakeholders.</p><p>In this step, the interviewer gauges your business sense. So, it is helpful to communicate your thought process and your reasons for choosing certain metrics.</p><h4>Talking points</h4><p>Here are some talking points for the evaluation step:</p><ul><li><strong>Online metrics:</strong> Which metrics are important for measuring the effectiveness of the ML system online? How do these metrics relate to the business objective?</li><li><strong>Offline metrics:</strong> Which offline metrics are good at evaluating the model’s predictions during the development phase?</li><li><strong>Fairness and bias:</strong> Does the model have the potential for bias across different attributes such as age, gender, race, etc.? How would you fix this? What happens if someone with malicious intent gets access to your system?</li></ul><h3>Deployment and Serving</h3><p>A natural next step after choosing the appropriate metrics for online and offline evaluations is to deploy the model to production and serve millions of users. Some important topics to cover include:</p><ul><li>Cloud vs. on-device deployment</li><li>Model compression</li><li>Testing in production</li><li>Prediction pipeline</li></ul><p>Let’s take a look at each.</p><h4>Cloud vs. on-device deployment</h4><p>Deploying a model on the cloud is different from deploying it on a mobile device. Table 1.6 summarizes the main differences between on-device deployment and cloud deployment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*cTopAjDEkaW4C4xAOZbn7g.png" /></figure><h4>Model compression</h4><p>Model compression refers to the process of making a model smaller. This is necessary to reduce the inference latency and model size. Three techniques are commonly used to compress models:</p><ul><li><strong>Knowledge distillation:</strong> The goal of knowledge distillation is to train a small model (student) to mimic a larger model (teacher).</li><li><strong>Pruning:</strong> Pruning refers to the process of finding the least useful parameters and setting them to zero. This leads to sparser models which can be stored more efficiently.</li><li><strong>Quantization:</strong> Model parameters are often represented with 32-bit floating numbers. In quantization, we use fewer bits to represent the parameters, which reduces the model’s size. Quantization can happen during training or post-training [37].</li></ul><p>To learn more about model compression, you are encouraged to read [38].</p><h4>Test in production</h4><p>The only way to ensure the model will perform well in production is to test it with real traffic. Commonly used techniques to test models include shadow deployment [39], A/B testing [40], canary release [41], interleaving experiments [42], bandits [43], etc.</p><p>To demonstrate that we understand how to test in production, it’s a good idea to mention at least one of these methods. Let’s briefly review shadow deployment and A/B testing.</p><p>Shadow deployment</p><p>In this method, we deploy the new model in parallel with the existing model. Each incoming request is routed to both models, but only the existing model’s prediction is served to the user.</p><p>By shadow deploying the model, we minimize the risk of unreliable predictions until the newly developed model has been thoroughly tested. However, this is a costly approach that doubles the number of predictions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/270/1*Hp6QDI7n3JCo72cNn8eCvw.png" /></figure><p>A/B testing</p><p>With this method, we deploy the new model in parallel with the existing model. A portion of the traffic is routed to the newly developed model, while the remaining requests are routed to the existing model.</p><p>In order to execute A/B testing correctly, there are two important factors to consider. First, the traffic routed to each model has to be random. Second, A/B tests should be run on a sufficient number of data points in order for the results to be legitimate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/285/1*_dWem3SBOv8v18EOku9qKA.png" /></figure><h4>Prediction pipeline</h4><p>To serve requests in production, we need a prediction pipeline. An important design decision to make is the choice between online prediction and batch prediction.</p><p><strong>Batch prediction.</strong> With batch prediction, the model makes predictions periodically. Because predictions are pre-computed, we don’t need to worry about how long it takes the model to generate predictions once they are pre-computed.</p><p>However, the batch prediction has two major drawbacks. First, the model becomes less responsive to the changing preferences of users. Secondly, batch prediction is only possible if we know in advance what needs to be pre-computed. For example, in a language translation system, we are not able to make translations in advance as it entirely depends on the user’s input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*Mh7o0IdycWpVhsID07Uolg.png" /></figure><p><strong>Online prediction.</strong> In online prediction, predictions are generated and returned as soon as requests arrive. The main problem with online prediction is that the model might take too long to generate predictions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*-rxKWARFbnxqgKluwgL2QQ.png" /></figure><p>This choice of batch prediction or online prediction is mainly driven by product requirements. Online prediction is generally preferred in situations where we do not know what needs to be computed in advance. Batch prediction is ideal when the system processes a high volume of data, and the results are not needed in real time.</p><p>As previously discussed, ML system development involves more than just ML modeling. Proposing an overall ML system design in an interview demonstrates a deep understanding of how different components work together as a whole. Interviewers often take this as a critical signal.</p><p>Figure 1.23 shows an example of the ML system design for a personalized news feed system. We will examine it in more depth in Chapter 10.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*wIc3RK8T2zOtaJM09ehAzg.png" /></figure><h4>Talking points</h4><ul><li>Is model compression needed? What are some commonly used compression techniques?</li><li>Is online prediction or batch prediction more suitable? What are the trade-offs?</li><li>Is real-time access to features possible? What are the challenges?</li><li>How should we test the deployed model in production?</li><li>An ML system consists of various components working together to serve requests. What are the responsibilities of each component in the proposed design?</li><li>What technologies should we use to ensure that serving is fast and scalable?</li></ul><h3>Monitoring</h3><p>Monitoring refers to the task of tracking, measuring, and logging different metrics. An ML system in production can fail for many reasons. Monitoring helps to detect system failures when they occur, so they can be fixed as quickly as possible.</p><p>Monitoring is an important topic to discuss in ML system design interviews. Two primary areas you may want to discuss are:</p><ul><li>Why a system fails in production</li><li>What to monitor</li></ul><h4>Why a system fails in production</h4><p>There are various reasons why an ML system may fail after it is deployed to production. One of the most common reasons is data distribution shift.</p><p>Data distribution shift refers to the scenario in which the data a model encounters in production differs from that it encountered during training. Figure 1.24 shows an example where the training data comprised images of cups seen from the front view, but at serving time, an image of a cup at a different angle is passed to the ML model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*b6P8HorIr7A7LypgJIwfwg.png" /></figure><p>Data distribution in the real world constantly changes. In other words, the data used for training is likely to become less relevant as time passes. This leads to a stale model with deteriorating performance over time. Therefore, we should continuously monitor the system to detect this problem. Two common approaches for dealing with data distribution shifts are:</p><ul><li>Train on large datasets. A big enough training dataset enables the model to learn a comprehensive distribution, so any data points encountered in production likely come from this learned distribution.</li><li>Regularly retrain the model using labeled data from the new distribution.</li></ul><h4>What to monitor</h4><p>This is a broad topic, and here we focus on post-production monitoring techniques. Our goal is to detect failures and identify shifts in the ML system. Broadly speaking, we can categorize monitoring techniques in ML systems into two buckets: operation-related and ML-specific metrics.</p><p><strong>Operation-related metrics:</strong> Those metrics ensure the system is up and running. They include average serving times, throughput, the number of prediction requests, CPU/GPU utilization, etc.</p><p><strong>ML-specific metrics:</strong></p><ul><li>Monitoring inputs/outputs. Models are only as good as the data they consume, so monitoring the model’s input and outputs is vital.</li><li>Drifts. Inputs to the system and the model’s outputs are monitored to detect changes in their underlying distribution.</li><li>Model accuracy. For example, we expect the accuracy to be within a specific range.</li><li>Model versions. Monitor which version of the model is deployed.</li></ul><h3>Infrastructure</h3><p>Infrastructure is the foundation for training, deploying, and maintaining ML systems.</p><p>In many ML interviews, you won’t be asked infrastructure-related questions. However, some ML roles, such as DevOps and MLOps may require infrastructure knowledge. So, it’s important to be clear about the interviewer’s expectations for this topic.</p><p>Infrastructure is a very broad subject and cannot be summarized in just a few lines. If you’re interested in learning more about ML infrastructure, refer to [44][45][46].</p><h3>Summary</h3><p>In this chapter, we proposed a framework for an ML system design interview. While many topics discussed in this chapter are task-specific, some are generic and applicable to a wide range of tasks. Throughout this book, we only focus on unique talking points specific to the problem at hand, in order to avoid repetition. For example, topics related to deployment, monitoring, and infrastructure are often similar, regardless of the task. Therefore, we do not repeat generic topics in later chapters, but you are usually expected to talk about them during an interview.</p><p>Finally, no engineer can be an expert in every aspect of the ML lifecycle. Some engineers specialize in deployment and production, while others specialize in model development. Some companies may not care about infrastructure, while others may focus heavily on monitoring and infrastructure. Data science roles generally require more data engineering, while applied ML roles focus more on model development and productionization. Depending on the role and the interviewer’s preference, some steps may be discussed in more detail, while others may be discussed briefly or even skipped. In general, a candidate should seek to drive the conversation, while being ready to go with the interviewer’s flow, if they raise a question.</p><p>Now you understand these fundamentals, we’re ready to tackle some of the most common ML system design interview questions.</p><h3>References</h3><ol><li>Data warehouse. <a href="https://cloud.google.com/learn/what-is-a-data-warehouse">https://cloud.google.com/learn/what-is-a-data-warehouse</a>.</li><li>Bagging technique in ensemble learning. <a href="https://en.wikipedia.org/wiki/Bootstrap_aggregating">https://en.wikipedia.org/wiki/Bootstrap_aggregating</a>.</li><li>Boosting technique in ensemble learning. <a href="https://aws.amazon.com/what-is/boosting/">https://aws.amazon.com/what-is/boosting/</a>.</li><li>Stacking technique in ensemble learning. <a href="https://machinelearningmastery.com/stacking-ensemble-machine-learning-with-python/">https://machinelearningmastery.com/stacking-ensemble-machine-learning-with-python/</a>.</li><li>Interpretability in Machine Mearning. <a href="https://blog.ml.cmu.edu/2020/08/31/6-interpretability/">https://blog.ml.cmu.edu/2020/08/31/6-interpretability/</a>.</li><li>Traditional machine learning algorithms. <a href="https://machinelearningmastery.com/a-tour-of-machine-learning-algorithms/">https://machinelearningmastery.com/a-tour-of-machine-learning-algorithms/</a>.</li><li>Sampling strategies. <a href="https://www.scribbr.com/methodology/sampling-methods/">https://www.scribbr.com/methodology/sampling-methods/</a>.</li><li>Data splitting techniques. <a href="https://machinelearningmastery.com/train-test-split-for-evaluating-machine-learning-algorithms/">https://machinelearningmastery.com/train-test-split-for-evaluating-machine-learning-algorithms/</a>.</li><li>Class-balanced loss. <a href="https://arxiv.org/pdf/1901.05555.pdf">https://arxiv.org/pdf/1901.05555.pdf</a>.</li><li>Focal loss paper. <a href="https://arxiv.org/pdf/1708.02002.pdf">https://arxiv.org/pdf/1708.02002.pdf</a>.</li><li>Focal loss. <a href="https://medium.com/swlh/focal-loss-an-efficient-way-of-handling-class-imbalance-4855ae1db4cb">https://medium.com/swlh/focal-loss-an-efficient-way-of-handling-class-imbalance-4855ae1db4cb</a>.</li><li>Data parallelism. <a href="https://www.telesens.co/2017/12/25/understanding-data-parallelism-in-machine-learning/">https://www.telesens.co/2017/12/25/understanding-data-parallelism-in-machine-learning/</a>.</li><li>Model parallelism. <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/model-parallel-intro.html">https://docs.aws.amazon.com/sagemaker/latest/dg/model-parallel-intro.html</a>.</li><li>Cross entropy loss. <a href="https://en.wikipedia.org/wiki/Cross_entropy">https://en.wikipedia.org/wiki/Cross_entropy</a>.</li><li>Mean squared error loss. <a href="https://en.wikipedia.org/wiki/Mean_squared_error">https://en.wikipedia.org/wiki/Mean_squared_error</a>.</li><li>Mean absolute error loss. <a href="https://en.wikipedia.org/wiki/Mean_absolute_error">https://en.wikipedia.org/wiki/Mean_absolute_error</a>.</li><li>Huber loss. <a href="https://en.wikipedia.org/wiki/Huber_loss">https://en.wikipedia.org/wiki/Huber_loss</a>.</li><li>L1 and l2 regularization. <a href="https://www.analyticssteps.com/blogs/l2-and-l1-regularization-machine-learning">https://www.analyticssteps.com/blogs/l2-and-l1-regularization-machine-learning</a>.</li><li>Entropy regularization. <a href="https://paperswithcode.com/method/entropy-regularization">https://paperswithcode.com/method/entropy-regularization</a>.</li><li>K-fold cross validation. <a href="https://en.wikipedia.org/wiki/Cross-validation_(statistics)">https://en.wikipedia.org/wiki/Cross-validation_(statistics)</a>.</li><li>Dropout paper. <a href="https://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf">https://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf</a>.</li><li>Stochastic gradient descent. <a href="https://en.wikipedia.org/wiki/Stochastic_gradient_descent">https://en.wikipedia.org/wiki/Stochastic_gradient_descent</a>.</li><li>AdaGrad optimization algorithm. <a href="https://optimization.cbe.cornell.edu/index.php?title=AdaGrad">https://optimization.cbe.cornell.edu/index.php?title=AdaGrad</a>.</li><li>Momentum optimization algorithm. <a href="https://optimization.cbe.cornell.edu/index.php?title=Momentum">https://optimization.cbe.cornell.edu/index.php?title=Momentum</a>.</li><li>RMSProp optimization algorithm. <a href="https://optimization.cbe.cornell.edu/index.php?title=RMSProp">https://optimization.cbe.cornell.edu/index.php?title=RMSProp</a>.</li><li>ELU activation function. <a href="https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#elu">https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#elu</a>.</li><li>ReLU activation function. <a href="https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#relu">https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#relu</a>.</li><li>Tanh activation function. <a href="https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#tanh">https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#tanh</a>.</li><li>Sigmoid activation function. <a href="https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#softmax">https://ml-cheatsheet.readthedocs.io/en/latest/activation_functions.html#softmax</a>.</li><li>FID score. <a href="https://en.wikipedia.org/wiki/Fr%C3%A9chet_inception_distance">https://en.wikipedia.org/wiki/Fr%C3%A9chet_inception_distance</a>.</li><li>Inception score. <a href="https://en.wikipedia.org/wiki/Inception_score">https://en.wikipedia.org/wiki/Inception_score</a>.</li><li>BLEU metrics. <a href="https://en.wikipedia.org/wiki/BLEU">https://en.wikipedia.org/wiki/BLEU</a>.</li><li>METEOR metrics. <a href="https://en.wikipedia.org/wiki/METEOR">https://en.wikipedia.org/wiki/METEOR</a>.</li><li>ROUGE score. <a href="https://en.wikipedia.org/wiki/ROUGE_(metric)">https://en.wikipedia.org/wiki/ROUGE_(metric)</a>.</li><li>CIDEr score. <a href="https://arxiv.org/pdf/1411.5726.pdf">https://arxiv.org/pdf/1411.5726.pdf</a>.</li><li>SPICE score. <a href="https://arxiv.org/pdf/1607.08822.pdf">https://arxiv.org/pdf/1607.08822.pdf</a>.</li><li>Quantization-aware training. <a href="https://pytorch.org/docs/stable/quantization.html">https://pytorch.org/docs/stable/quantization.html</a>.</li><li>Model compression survey. <a href="https://arxiv.org/pdf/1710.09282.pdf">https://arxiv.org/pdf/1710.09282.pdf</a>.</li><li>Shadow deployment. <a href="https://christophergs.com/machine%20learning/2019/03/30/deploying-machine-learning-applications-in-shadow-mode/">https://christophergs.com/machine%20learning/2019/03/30/deploying-machine-learning-applications-in-shadow-mode/</a>.</li><li>A/B testing. <a href="https://en.wikipedia.org/wiki/A/B_testing">https://en.wikipedia.org/wiki/A/B_testing</a>.</li><li>Canary release. <a href="https://blog.getambassador.io/cloud-native-patterns-canary-release-1cb8f82d371a">https://blog.getambassador.io/cloud-native-patterns-canary-release-1cb8f82d371a</a>.</li><li>Interleaving experiment. <a href="https://netflixtechblog.com/interleaving-in-online-experiments-at-netflix-a04ee392ec55">https://netflixtechblog.com/interleaving-in-online-experiments-at-netflix-a04ee392ec55</a>.</li><li>Multi-armed bandit. <a href="https://vwo.com/blog/multi-armed-bandit-algorithm/">https://vwo.com/blog/multi-armed-bandit-algorithm/</a>.</li><li>ML infrastructure. <a href="https://www.run.ai/guides/machine-learning-engineering/machine-learning-infrastructure">https://www.run.ai/guides/machine-learning-engineering/machine-learning-infrastructure</a>.</li><li>Interpretability in ML. <a href="https://fullstackdeeplearning.com/spring2021/lecture-6/">https://fullstackdeeplearning.com/spring2021/lecture-6/</a>.</li><li>Chip Huyen. <em>Designing Machine Learning Systems: An Iterative Process for Production-Ready Application</em>. ” O’Reilly Media, Inc.”, 2022.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d72881022c2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object-Oriented Design Interview — Restaurant Management System]]></title>
            <link>https://medium.com/@dibasbehera/object-oriented-design-interview-restaurant-management-system-ba7b58286b5e?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba7b58286b5e</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Sun, 17 May 2026 18:56:00 GMT</pubDate>
            <atom:updated>2026-05-17T18:56:00.558Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design a Restaurant Management System</h3><p>In this chapter, we will explore the design of a Restaurant Management System. The goal is to create classes that represent the system’s essential components, such as menus, reservations, and tables. We will develop a system that supports critical functions like booking reservations, managing orders, and assigning tables, ensuring the design is both straightforward and flexible for future enhancements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*4sqQpfXE778mKiY2H7MyXQ.png" /></figure><h3>Requirements Gathering</h3><p>The first step in designing a Restaurant Management System is to clarify the requirements and define the scope. Here’s an example of a typical prompt an interviewer might present:</p><p>“Picture yourself planning a dinner outing on a Friday night. You call the restaurant to reserve a table for your group, check available times, and secure a spot. When you arrive, the staff assigns your party to the reserved table, takes your order, and later presents the bill. Behind the scenes, the system is smoothly handling table reservations, tracking orders, and calculating costs. Now, let’s design a Restaurant Management System that manages all of this.”</p><h3>Requirements clarification</h3><p>Here is an example of how a conversation between a candidate and an interviewer might unfold:</p><p><strong>Candidate</strong>: Let’s start by setting the scope. I assume the system manages reservations, menu, order tracking, and payments. For now, I focus on reservations and order management. Does that work?<br><strong>Interviewer</strong>: That’s a reasonable starting point</p><p><strong>Candidate</strong>: Does the system allow customers to make and manage their reservations?<br><strong>Interviewer</strong>: Yes, customers can book tables for a future date and time based on availability.</p><p><strong>Candidate</strong>: How does the system determine if a table is available for a reservation?<br><strong>Interviewer</strong>: It checks for a table that fits the party size and is free at the requested time. Each reservation reserves a table for exactly one hour, so it’s available if no other booking overlaps with that hour.</p><p><strong>Candidate</strong>: Does the system let customers cancel a reservation after making it?<br><strong>Interviewer</strong>: Yes, customers can cancel reservations.</p><p><strong>Candidate</strong>: When a party with a reservation arrives, do they automatically get their reserved table?<br><strong>Interviewer</strong>: Yes, they do. They arrive with their name, and the system uses it to find their reservation, which already has a table assigned to it.</p><p><strong>Candidate</strong>: What happens when a walk-in party arrives for dine-in without prior reservation?<br><strong>Interviewer</strong>: The system should assign walk-in parties to tables based on current availability and their party size.</p><p><strong>Candidate</strong>: Does the system allow orders to be altered or removed after they’re placed?<br><strong>Interviewer</strong>: Yes, you can remove items or adjust their quantities.</p><p><strong>Candidate</strong>: Does the system track the status of orders?<br><strong>Interviewer</strong>: Yes, it keeps track of their progress.</p><p><strong>Candidate</strong>: Are there rules for splitting the bill at checkout?<br><strong>Interviewer</strong>: For now, just present a single total bill amount.</p><h3>Requirements</h3><p>Based on the questions and answers, we can now list the functional requirements for our restaurant management system.</p><p><strong>Reservations</strong></p><ul><li>Customers can book tables for a future date and time based on availability.</li><li>Each reservation reserves a table for exactly one hour.</li><li>The system checks if a table is free at the requested time with no overlapping bookings.</li><li>The system assigns a table to a reservation, and customers get it when they arrive using their name.</li><li>Customers can cancel reservations.</li></ul><p><strong>Walk-in seating</strong></p><ul><li>The system assigns walk-in parties to tables based on current availability and party size.</li></ul><p><strong>Order management</strong></p><ul><li>The system allows orders to be altered or removed after they’re placed.</li><li>The system tracks the progress of orders.</li></ul><p><strong>Billing</strong></p><ul><li>The system presents a single total bill amount at checkout.</li></ul><p>Below are the non-functional requirements:</p><ul><li>The system should handle increased traffic during busy periods (e.g., weekend evenings) without performance degradation, supporting concurrent users seamlessly.</li></ul><p>With these requirements, we are ready to model the objects for the core system.</p><h3>Identify Core Objects</h3><p>Let’s identify the core objects of the restaurant management system.</p><ul><li><strong>Menu:</strong> Represents the restaurant’s menu, storing a collection of available items for ordering.</li><li><strong>MenuItem:</strong> This object models an individual item on the menu, encapsulating details such as its name and price. Each MenuItem is a building block of the Menu, used when staff place orders for a table.</li><li><strong>Layout:</strong> This object represents the restaurant’s physical arrangement, organizing all tables efficiently to support quick and effective assignment for both walk-ins and reservations.</li><li><strong>Table:</strong> This object models an individual table in the restaurant, holding details such as its capacity, current reservations, and active orders.</li><li><strong>Reservation:</strong> This object represents a single reservation, storing details like the party name, party size, and reserved time, along with the assigned table.</li><li><strong>ReservationManager:</strong> ​​This object oversees all restaurant reservations, managing their creation, lookup, and cancellation to ensure accurate and efficient booking. It checks table availability for free time slots and works with Layout to assign tables, keeping reservations well-scheduled and tracked.</li><li><strong>Restaurant:</strong> This acts as a facade, providing a central interface to manage the system’s key functionalities: reservations, table assignments, orders, and bill calculations. We keep its logic lightweight by delegating tasks like scheduling reservations to ReservationManager, assigning tables to Layout, and managing orders to Table, ensuring it coordinates actions without performing the underlying operations itself.</li></ul><h3>Design Class Diagram</h3><p>You’re set to dive into the heart of the object-oriented design interview: crafting classes and interfaces, shaping data and state through attributes, wrapping logic in methods, and linking your classes with clear relationships.</p><p>Below, we detail each class, its purpose, and its responsibilities, ensuring a clear separation of concerns.</p><h3>Menu</h3><p>The Menu class represents the restaurant’s menu, storing menu items in a map with names as keys to quickly retrieve them for ordering. It separates menu data from the Restaurant and Table classes, enabling Table to order items by holding a collection of MenuItem objects that list all available choices.</p><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*U0s4u8M26D4F0NtzxCWgLw.png" /></figure><h3>MenuItem</h3><p>The MenuItem defines each item on the menu, holding its name, description, price, and category for use in orders. The Menu class uses these items to provide the list of choices, and the Table class records them as ordered items for order management and updates. The Category enum assigns each item a type, such as main course, appetizer, or dessert, to group them on the menu.</p><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*8yIRrPnijP0bJKSYAbE0tw.png" /></figure><h3>Table</h3><p>The Table class models restaurant tables. Some of the attributes, like capacity and tableId, rarely change. But other attributes, like reservations and orderedItems, associated with the table represent current-state data that changes over time.</p><p>Its purpose is to oversee a table’s current use, tying into the Layout class for availability checks and the OrderItem class to handle what’s being served. It includes methods to add or remove orders, tally up bills, and check availability at specific times.</p><p>Here is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*-OXX-W_rxkfDy1p2wSu5zQ.png" /></figure><h3>Layout</h3><p>The Layout class oversees all restaurant tables, organizing them by ID and capacity to pinpoint the right one for each booking. Its purpose is to streamline table assignments, working with ReservationManager to match parties to available tables and relying on Table to confirm free slots. It handles this by finding a table that fits the party size and is available at the requested time, keeping the process efficient.</p><p><strong>Design choice:</strong> We isolate table organization in the Layout class to optimize assignment efficiency and separate it from menu and order logic managed by the Menu and Table classes. Alternatively, integrating table assignment into the Restaurant class could simplify the design but would overburden its facade role, mixing high-level coordination with low-level table management.</p><p>Below is the representation of the Layout class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*yf9wx7n0jznAqAkgOfFQHg.png" /></figure><h3>OrderItem</h3><p>The OrderItem class represents each item a customer orders, linking it to a specific MenuItem to provide details like price for the Table class. Its purpose is to track the status of ordered items, allowing the Table class to calculate costs accurately and add or remove items as requested. The class is created when customers place orders, using the Status enum, set to values like pending or delivered, to indicate the item’s current state.</p><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*nfhoCtLdUZqHbJNsJ5uATg.png" /></figure><h3>ReservationManager</h3><p>The ReservationManager class handles reservation scheduling by finding available times, creating reservations, and processing cancellations. It stores all Reservation objects and uses a reference to the Layout class to manage table assignments. This reference enables the class to verify table availability and assign suitable tables for each reservation based on party size and time.</p><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*Q4j9e2NeGpa-RfwoHK23Vw.png" /></figure><h3>Reservation</h3><p>The Reservation class represents a single booking managed by the ReservationManager class, storing the party name, number of people, reservation time, and assigned table. It serves to hold all details of a reservation, enabling the ReservationManager class to schedule and cancel bookings effectively.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/211/1*r_AgmupWWl9v8FacXgDXYA.png" /></figure><p>The class is contained within ReservationManager as one of its entries, maintaining a structured and accessible set of reservation data.</p><h3>Restaurant</h3><p>The Restaurant class serves as the primary interface and facade for the restaurant management system. It coordinates core user-facing operations, such as managing reservations, assigning tables, processing orders, and handling checkout.</p><p>It simplifies access to these features by delegating tasks to other classes:</p><ul><li>The class relies on ReservationManager to schedule and cancel reservations.</li><li>The class uses Layout through ReservationManager to identify available tables for assignments.</li><li>The class depends on Menu to supply items for orders.</li><li>The class directs order and billing actions to the Table for processing.</li></ul><p>This delegation keeps the Restaurant class focused and manageable, organizing the system into distinct components that ensure clarity and ease of maintenance through a structured use of composition.</p><p><strong>Design choice:</strong> We structure the Restaurant as a facade to unify system operations, delegating tasks to maintain a clean interface and modularity. We can design the Restaurant class as a central controller managing all logic internally, but that would increase its complexity and reduce scalability by centralizing responsibilities.</p><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/496/1*2YW-pf5CONWn9v7uyduItQ.png" /></figure><p>Next, we’ll connect these objects in a class diagram to visualize their relationships.</p><h3>Complete Class Diagram</h3><p>Below is the complete class diagram of our restaurant management system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/544/1*dY859Vhcpsq9L1EaNMFBjg.png" /></figure><p>With this structure in place, let’s move on to implement the code that brings this design to life.</p><h3>Code — Restaurant Management System</h3><p>In this section, we’ll implement the core functionalities of the Restaurant Management System, focusing on key areas such as managing menu items, scheduling and canceling reservations, assigning tables for bookings and walk-ins, and processing orders with billing through a table-based system.</p><h3>Menu</h3><p>The Menu class manages the restaurant’s menu by storing MenuItem objects in a HashMap, using each item’s name as the key to enable fast retrieval for order processing. It provides methods to add an item with addItem, retrieve a specific item using getItem, and access the full menu through getMenuItems as a read-only view.</p><p><strong>Implementation Choice:</strong> We selected HashMap to store MenuItem objects for its quick lookup performance through key-based access, ensuring efficient retrieval for the Table class during order processing, whereas a List alternative, while simpler for storage, requires a linear search that slows access time.</p><p>The definition of the Menu class is given below.</p><pre>public class Menu {<br>    private final Map&lt;String, MenuItem&gt; menuItems = new HashMap&lt;&gt;();<br><br>    // Adds a new item to the menu<br>    public void addItem(MenuItem item) {<br>        menuItems.put(item.getName(), item);<br>    }<br><br>    public MenuItem getItem(String name) {<br>        return menuItems.get(name);<br>    }<br><br>    public Map&lt;String, MenuItem&gt; getMenuItems() {<br>        return Collections.unmodifiableMap(menuItems);<br>    }<br>}</pre><p>With the Menu defined, let’s detail its individual items in MenuItem.</p><h3>MenuItem</h3><p>The MenuItem class represents an individual item on the menu, storing its name, description, price, and category as private final fields to ensure immutability. This class uses BigDecimal for the price field instead of a floating-point type like float or double. BigDecimal is better suited for financial data, as it provides precise control over decimal values and helps avoid rounding errors or precision issues that can arise with floating-point calculations.</p><pre>// Represents a single item available on the restaurant menu<br>public class MenuItem {<br>    private final String name;<br>    private final String description;<br>    private final BigDecimal price;<br>    private final Category category;<br><br>    public MenuItem(String name, String description, BigDecimal price, Category category) {<br>        this.name = name;<br>        this.description = description;<br>        this.price = price;<br>        this.category = category;<br>    }<br><br>    // Enumeration of possible menu item categories<br>    public enum Category {<br>        MAIN,<br>        APPETIZER,<br>        DESSERT<br>    } // getter methods are omitted for brevity<br>}</pre><p>Having established the menu’s contents, let’s model the restaurant’s tables with Table.</p><h3>Table</h3><p>The Table class represents a restaurant table, storing its fixed properties, such as tableId and capacity, while maintaining its bookings and orders by updating reservations and orderedItems. Its purpose is to manage a table’s reservations and orders, using Layout to confirm available time slots and OrderItem to provide price and status details.</p><p>Below is the implementation of this class.</p><pre>// Represents a table in the restaurant with its properties and current state<br>public class Table {<br>    // immutable properties<br>    private final int tableId;<br>    private final int capacity;<br><br>    // current state<br>    private final Map&lt;LocalDateTime, Reservation&gt; reservations = new HashMap&lt;&gt;();<br>    private final Map&lt;MenuItem, List&lt;OrderItem&gt;&gt; orderedItems = new HashMap&lt;&gt;();<br><br>    public Table(int tableId, int capacity) {<br>        this.tableId = tableId;<br>        this.capacity = capacity;<br>    }<br><br>    // Calculates the total bill amount for all ordered items at this table<br>    public BigDecimal calculateBillAmount() {<br>        return orderedItems.values().stream()<br>                .flatMap(List::stream)<br>                .map(OrderItem::getItem)<br>                .map(MenuItem::getPrice)<br>                .reduce(BigDecimal.ZERO, BigDecimal::add);<br>    }<br><br>    // Adds multiple orders of the same menu item to the table<br>    public void addOrder(MenuItem item, int quantity) {<br>        for (int i = 0; i &lt; quantity; i++) {<br>            addOrder(item);<br>        }<br>    }<br><br>    // Adds a single menu item to the table&#39;s order<br>    public void addOrder(MenuItem item) {<br>        List&lt;OrderItem&gt; orderItems = orderedItems.get(item);<br>        if (orderItems == null) {<br>            orderItems = new ArrayList&lt;&gt;();<br>            orderedItems.put(item, orderItems);<br>            orderItems.add(new OrderItem(item));<br>        } else {<br>            orderItems.add(new OrderItem(item));<br>        }<br>    }<br><br>    // Removes a menu item from the table&#39;s order<br>    public void removeOrder(MenuItem item) {<br>        List&lt;OrderItem&gt; orderItems = orderedItems.get(item);<br>        if (orderItems != null) {<br>            orderItems.remove(0);<br>            if (orderItems.isEmpty()) {<br>                orderedItems.remove(item);<br>            }<br>        }<br>    }<br><br>    // Checks if the table is available at a specific time<br>    public boolean isAvailableAt(LocalDateTime reservationTime) {<br>        return !reservations.containsKey(reservationTime);<br>    }<br><br>    // Adds a reservation to this table<br>    public void addReservation(Reservation reservation) {<br>        reservations.put(reservation.getTime(), reservation);<br>    }<br><br>    // Removes a reservation from this table for a specific time<br>    public void removeReservation(LocalDateTime reservationTime) {<br>        reservations.remove(reservationTime);<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><p>The class uses a Map with reservation times as keys to store reservations. This setup enables quick availability checks through the isAvailableAt method. It also uses a separate Map with MenuItem keys to manage orderedItems. This structure supports the addOrder method to include items, the removeOrder method to remove them, and the calculateBillAmount method for billing.</p><p>With tables in place, let’s organize them efficiently using Layout.</p><h3>Layout</h3><p>The Layout class represents the seating arrangement of the entire restaurant, managing all tables to support efficient assignments for reservations and walk-ins. It organizes tables using two indexing methods:</p><ul><li><strong>Tables by ID</strong>: Stored in a Map (tablesById) with each table’s ID as the key. This method allows fast retrieval of a table by its unique identifier during operations.</li><li><strong>Tables by Capacity</strong>: Stored in a SortedMap (tablesByCapacity) where each capacity level maps to a set of tables with that capacity. This method enables the findAvailableTable method to locate the smallest available table that fits a party’s size quickly.</li></ul><p>Below is the implementation of this class.</p><pre>// Manages the collection of tables in the restaurant and their arrangement<br>public class Layout {<br>    private final Map&lt;Integer, Table&gt; tablesById = new HashMap&lt;&gt;();<br>    // Groups tables by their capacity for efficient table assignment, sorted from smallest to<br>    // largest capacity<br>    private final SortedMap&lt;Integer, Set&lt;Table&gt;&gt; tablesByCapacity = new TreeMap&lt;&gt;();<br><br>    public Layout(List&lt;Integer&gt; tableCapacities) {<br>        for (int i = 0; i &lt; tableCapacities.size(); i++) {<br>            int capacity = tableCapacities.get(i);<br>            Table table = new Table(i, capacity);<br>            tablesById.put(i, table);<br>            tablesByCapacity.computeIfAbsent(capacity, k -&gt; new HashSet&lt;&gt;()).add(table);<br>        }<br>    }<br><br>    // Finds the smallest available table that can accommodate a party of the given size at the<br>    // given time<br>    public Table findAvailableTable(int partySize, LocalDateTime reservationTime) {<br>        for (Set&lt;Table&gt; tables : tablesByCapacity.tailMap(partySize).values()) {<br>            for (Table table : tables) {<br>                if (table.isAvailableAt(reservationTime)) {<br>                    return table;<br>                }<br>            }<br>        }<br>        return null;<br>    }<br>}</pre><p><strong>Implementation Choice:</strong> We chose SortedMap structures for tablesByCapacity. This structure provides sorted key access that supports efficient range searches by capacity. This efficiency is crucial for the findAvailableTable method to match tables to party sizes. A basic Map lacks sorting capability. It would require additional logic to identify the smallest suitable table, making it less efficient.</p><p>Now that tables are arranged, let’s manage individual order items with OrderItem.</p><h3>OrderItem</h3><p>The OrderItem class defines each item ordered by a customer, storing a reference to a specific MenuItem and maintaining its current status to track its state during order processing.</p><p>The class includes methods to update the order’s state:</p><ul><li><strong>sendToKitchen</strong> changes the status from PENDING to SENT_TO_KITCHEN</li><li><strong>deliverToCustomer</strong> updates it from SENT_TO_KITCHEN to DELIVERED</li><li><strong>cancel</strong> sets it to CANCELED if not yet delivered</li></ul><pre>// Represents a food item ordered by a customer with its current status in the order process<br>public class OrderItem {<br>    private final MenuItem item;<br>    private Status status = Status.PENDING;<br><br>    public OrderItem(MenuItem item) {<br>        this.item = item;<br>    }<br><br>    // Updates the status to indicate the item has been sent to the kitchen<br>    public void sendToKitchen() {<br>        if (status == Status.PENDING) status = Status.SENT_TO_KITCHEN;<br>    }<br><br>    // Updates the status to indicate the item has been delivered to the customer<br>    public void deliverToCustomer() {<br>        if (status == Status.SENT_TO_KITCHEN) status = Status.DELIVERED;<br>    }<br><br>    // Updates the status to indicate the item has been canceled<br>    public void cancel() {<br>        if (status == Status.PENDING || status == Status.SENT_TO_KITCHEN) {<br>            status = Status.CANCELED;<br>        }<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><p>With orders tracked, let’s oversee reservations through ReservationManager.</p><h3>ReservationManager</h3><p>The ReservationManager class oversees all reservations in the restaurant, managing their scheduling, creation, and cancellation to ensure tables are assigned accurately. It serves as a central coordinator, connecting to the Layout class to locate available tables and storing Reservation objects to maintain booking details.</p><p>Here is the implementation of this class.</p><pre>// Manages all reservations for the restaurant and handles table assignments<br>public class ReservationManager {<br>    private final Layout layout;<br>    private final Set&lt;Reservation&gt; reservations = new HashSet&lt;&gt;();<br><br>    // Constructor that takes the restaurant&#39;s table layout<br>    public ReservationManager(Layout layout) {<br>        this.layout = layout;<br>    }<br><br>    // Finds potential time slots for a reservation within the given time range and party size<br>    public LocalDateTime[] findAvailableTimeSlots(<br>            LocalDateTime rangeStart, LocalDateTime rangeEnd, int partySize) {<br>        // checking every hour in the time range<br>        LocalDateTime current = rangeStart;<br>        List&lt;LocalDateTime&gt; possibleReservations = new ArrayList&lt;&gt;();<br>        while (!current.isAfter(rangeEnd)) {<br>            Table availableTable = layout.findAvailableTable(partySize, current);<br>            if (availableTable != null) {<br>                possibleReservations.add(current);<br>            }<br>            current = current.plusHours(1);<br>        }<br>        return possibleReservations.toArray(new LocalDateTime[0]);<br>    }<br><br>    // Creates a reservation for a specific time, party size and name<br>    public Reservation createReservation(<br>            String partyName, int partySize, LocalDateTime desiredTime) {<br>        desiredTime = desiredTime.truncatedTo(ChronoUnit.HOURS);<br>        Table table = layout.findAvailableTable(partySize, desiredTime);<br>        Reservation reservation = new Reservation(partyName, partySize, desiredTime, table);<br>        table.addReservation(reservation);<br>        reservations.add(reservation);<br>        return reservation;<br>    }<br><br>    // Removes an existing reservation<br>    public void removeReservation(<br>            String partyName, int partySize, LocalDateTime reservationTime) {<br>        // Find matching reservation before removing it<br>        for (Reservation reservation : new HashSet&lt;&gt;(reservations)) {<br>            if (reservation.getTime().equals(reservationTime)<br>                    &amp;&amp; reservation.getPartySize() == partySize<br>                    &amp;&amp; reservation.getPartyName().equals(partyName)) {<br>                // Clear the reservation from the table first<br>                Table table = reservation.getAssignedTable();<br>                table.removeReservation(reservationTime);<br>                // Then remove from the reservation collection<br>                reservations.remove(reservation);<br>                return;<br>            }<br>        }<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><ul><li><strong>findAvailableTimeSlots</strong>: This method identifies available reservation times within a specified range. It examines each hour in the range, assuming a one-hour duration per reservation.</li><li><strong>createReservation</strong>: This method creates a reservation for a given time, party size, and party name. It uses Layout.findAvailableTable to confirm a table’s availability. If a table is found, it generates a new Reservation object, adds it to the reservations set, and updates the table’s booking status.</li><li><strong>removeReservation</strong>: This method cancels a reservation based on its time, party size, and party name. It searches the reservations set to locate the matching entry, removes it, and clears the table’s booking if found.</li></ul><p><strong>Implementation Choice:</strong> We chose a Set to store Reservation objects because its unique entry enforcement prevents duplicate bookings, aligning with the need to manage reservations accurately. An alternative could use a List, which allows simpler iteration but risks duplicates unless additional checks are added, or a Map with time-based keys, which could speed up lookups but complicate removal by requiring keys that combine multiple fields, like time, party size, and name, less suited for the system’s focus on reservation uniqueness.</p><p>Next, let’s define the reservation entries with Reservation.</p><h3>Reservation</h3><p>The Reservation class is a simple, immutable entity class that stores essential reservation details, including the party name, number of people, reservation time, and assigned table. It serves as the foundational unit for the ReservationManager class, holding the data needed to manage bookings effectively.</p><pre>// Represents a reservation made at the restaurant for a specific party, time and table<br>public class Reservation {<br>    private final String partyName;<br>    private final int partySize;<br>    private final LocalDateTime time;<br>    private final Table assignedTable;<br><br>    public Reservation(<br>            String partyName, int partySize, LocalDateTime time, Table assignedTable) {<br>        this.partyName = partyName;<br>        this.partySize = partySize;<br>        this.time = time;<br>        this.assignedTable = assignedTable;<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><p>Finally, let’s unify these components in the Restaurant.</p><h3>Restaurant</h3><p>The Restaurant class acts as the central interface for the restaurant management system, offering methods to book reservations, seat walk-in parties, place orders, and compute bills. It follows the facade design pattern to unify these features, delegating reservation and walk-in seating tasks to ReservationManager, which uses Layout to find available tables, order placement, and billing to Table, which uses Menu to access items.</p><p>Below is the implementation of this class.</p><pre>// Main restaurant class that manages reservations, orders, and tables<br>public class Restaurant {<br>    private final String name;<br>    private final Menu menu;<br>    private final Layout layout;<br>    private final ReservationManager reservationManager;<br><br>    public Restaurant(String name, Menu menu, Layout layout) {<br>        this.name = name;<br>        this.menu = menu;<br>        this.layout = layout;<br>        this.reservationManager = new ReservationManager(layout);<br>    }<br><br>    // Finds possible reservation times within a time range for a party of specified size<br>    public LocalDateTime[] findAvailableTimeSlots(<br>            LocalDateTime rangeStart, LocalDateTime rangeEnd, int partySize) {<br>        return reservationManager.findAvailableTimeSlots(rangeStart, rangeEnd, partySize);<br>    }<br><br>    // Creates a reservation for a party at the specified time<br>    public Reservation createScheduledReservation(<br>            String partyName, int partySize, LocalDateTime time) {<br>        return reservationManager.createReservation(partyName, partySize, time);<br>    }<br><br>    // Removes an existing reservation<br>    public void removeReservation(<br>            String partyName, int partySize, LocalDateTime reservationTime) {<br>        reservationManager.removeReservation(partyName, partySize, reservationTime);<br>    }<br><br>    // Creates a reservation for a party without prior reservation<br>    public Reservation createWalkInReservation(String partyName, int partySize) {<br>        return reservationManager.createReservation(partyName, partySize, LocalDateTime.now());<br>    }<br><br>    // Adds an item to a table&#39;s order<br>    public void orderItem(Table table, MenuItem item) {<br>        table.addOrder(item);<br>    }<br><br>    // Removes an item from a table&#39;s order<br>    public void cancelItem(Table table, MenuItem item) {<br>        table.removeOrder(item);<br>    }<br><br>    // Calculates the bill amount for a table<br>    public BigDecimal calculateTableBill(Table table) {<br>        return table.calculateBillAmount();<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><ul><li><strong>Reservation methods</strong>: The Restaurant class includes findAvailableTimeSlots to list available times, createScheduledReservation to book reservations, and removeReservation to cancel them, each delegating to ReservationManager for scheduling and management.</li><li><strong>Walk-in seating</strong>: The createWalkInReservation method assigns tables to walk-in parties, delegating to ReservationManager, which uses Layout to locate available tables.</li><li><strong>Order management</strong>: The orderItem method adds items to a table’s order, and cancelItem removes them, both delegating to Table methods addOrder and removeOrder for order processing.</li><li><strong>Checkout</strong>: The calculateTableBill method computes a table’s total cost, delegating to Table.calculateBillAmount for billing calculations.</li></ul><p><strong>Implementation Choice:</strong> We designed the Restaurant as a facade without its data structures. It delegates all operations to Menu, Layout, ReservationManager, and Table. This approach maintains a lightweight structure that simplifies system access. An alternative could have Restaurant store reservations or orders internally using a Map or List. That design would increase complexity and reduce modularity by centralizing responsibilities.</p><p>Having built the core system, let’s explore an enhancement in the deep dive to extend its capabilities.</p><h3>Deep Dive Topics</h3><p>In this section, we’ll explore an enhancement to the Restaurant Management System by improving order handling during peak times. We’ll focus on adding a centralized order queue tracking mechanism to streamline kitchen coordination, ensure scalability, and maintain consistency with the system’s modular design.</p><h3>Order queue tracking</h3><p>Consider a high-traffic scenario, such as a busy Friday evening at the restaurant, where a significant volume of orders is received, staff work diligently to communicate these to the kitchen, and cancellations accumulate. In the existing system, the Table class directly governs the status of OrderItem instances (e.g., through methods like sendToKitchen() and deliverToCustomer()). However, this decentralized structure lacks a cohesive overview of order progression across all tables. Consequently, staff face challenges in prioritizing time-sensitive orders, monitoring kitchen delays, or verifying cancellations without individually inspecting each table’s state. This approach introduces risks of inconsistency and undermines effective coordination during periods of elevated demand.</p><p>To address these limitations, we propose an enhancement by introducing a centralized OrderManager class responsible for queuing and processing order-related actions. Let’s take a closer look.</p><p><strong>Implementation steps:</strong></p><p>To implement this enhancement effectively, we’ll follow these steps:</p><p><strong>Step 1: Define a Command Interface for Order Actions</strong>: The staff needs a consistent way to issue actions like sending an order to the kitchen or canceling it, improving reliability over direct calls. We define an interface called OrderCommand with a single method, execute(), which concrete classes will implement to perform their tasks.</p><p>Here’s the code for the OrderCommand interface:</p><pre>public interface OrderCommand {<br>    void execute();<br>}</pre><p><strong>Step 2: Implement Concrete Command Classes</strong>: To give staff a flexible way to manage distinct order actions and prepare them for centralized queuing, we build specific classes for each task, unlike the original system, where Table directly updated OrderItem statuses.</p><ul><li>The SendToKitchenCommand class takes an OrderItem and, when executed, calls sendToKitchen() to update its status to SENT_TO_KITCHEN.</li><li>The DeliverCommand class uses deliverToCustomer() to change the OrderItem’s status to DELIVERED, signaling completion.</li><li>The CancelCommand class invokes cancel() to set the status to CANCELED if the item hasn’t been delivered, marking it as voided.</li></ul><p>Here’s the code for the action classes:</p><pre>// Command that handles sending order items to the Kitchen<br>public class SendToKitchenCommand implements OrderCommand {<br>    private final OrderItem orderItem;<br><br>    public SendToKitchenCommand(OrderItem orderItem) {<br>        this.orderItem = orderItem;<br>    }<br><br>    @Override<br>    public void execute() {<br>        orderItem.sendToKitchen();<br>    }<br>} // Command that handles delivery of order items<br><br>public class DeliverCommand implements OrderCommand {<br>    private final OrderItem orderItem;<br><br>    public DeliverCommand(OrderItem orderItem) {<br>        this.orderItem = orderItem;<br>    }<br><br>    @Override<br>    public void execute() {<br>        orderItem.deliverToCustomer();<br>    }<br>} // Command that handles cancellations of order items<br><br>public class CancelCommand implements OrderCommand {<br>    private final OrderItem orderItem;<br><br>    public CancelCommand(OrderItem orderItem) {<br>        this.orderItem = orderItem;<br>    }<br><br>    @Override<br>    public void execute() {<br>        orderItem.cancel();<br>    }<br>}</pre><p><strong>Step 3: Introduce the OrderManager Class</strong>: To handle these actions efficiently, we introduce an OrderManager class. This class maintains a list of OrderCommand objects, allowing us to add commands with addCommand() as orders are placed. The executeCommands() method processes all queued commands in sequence and clears the list afterward, ensuring orders are managed in an organized way without directly altering the Table’s OrderItems.</p><p>Here’s the code for the OrderManager class:</p><pre>public class OrderManager {<br>    private final List&lt;OrderCommand&gt; commandQueue = new ArrayList&lt;&gt;();<br><br>    // Adds a command to the queue for later execution<br>    public void addCommand(OrderCommand command) {<br>        commandQueue.add(command);<br>    }<br><br>    // Executes all commands in the queue and clears it<br>    public void executeCommands() {<br>        for (OrderCommand command : commandQueue) {<br>            command.execute();<br>        }<br>        commandQueue.clear();<br>    }<br>}</pre><p><strong>Step 4: Integrate with the Restaurant Class</strong>: To connect this system to staff actions, we update the Restaurant class to use OrderManager for order handling. When staff place an order, orderItem adds it to the table and queues a SendToKitchenCommand to send it to the kitchen. Similarly, cancelItem and deliverItem queue their respective commands, ensuring all actions flow through the centralized system for consistent tracking and execution.</p><p>Here’s the essential code for integrating OrderManager into the Restaurant:</p><pre>public class Restaurant {<br>    // ... fields unchanged ...<br>    private final OrderManager orderManager;<br><br>    public Restaurant(String name, Menu menu, Layout layout) {<br>        // ... fields unchanged ...<br>        this.orderManager = new OrderManager();<br>    }<br><br>    // Adds an item to a table&#39;s order and sends it to the kitchen<br>    public void orderItem(Table table, MenuItem item) {<br>        table.addOrder(item);<br>        // Get the last added order item<br>        List&lt;OrderItem&gt; orderItems = table.getOrderedItems().get(item);<br>        if (orderItems != null &amp;&amp; !orderItems.isEmpty()) {<br>            OrderItem lastOrder = orderItems.get(orderItems.size() - 1);<br>            OrderCommand sendToKitchen = new SendToKitchenCommand(lastOrder);<br>            orderManager.addCommand(sendToKitchen);<br>            orderManager.executeCommands();<br>        }<br>    }<br><br>    // Removes an item from a table&#39;s order and cancels it<br>    public void cancelItem(Table table, MenuItem item) {<br>        List&lt;OrderItem&gt; orderItems = table.getOrderedItems().get(item);<br>        if (orderItems != null &amp;&amp; !orderItems.isEmpty()) {<br>            OrderItem lastOrder = orderItems.get(orderItems.size() - 1);<br>            OrderCommand cancelOrder = new CancelCommand(lastOrder);<br>            orderManager.addCommand(cancelOrder);<br>            orderManager.executeCommands();<br>            table.removeOrder(item);<br>        }<br>    }<br><br>    // Delivers an item to the customer<br>    public void deliverItem(Table table, MenuItem item) {<br>        List&lt;OrderItem&gt; orderItems = table.getOrderedItems().get(item);<br>        if (orderItems != null &amp;&amp; !orderItems.isEmpty()) {<br>            OrderItem lastOrder = orderItems.get(orderItems.size() - 1);<br>            OrderCommand deliverOrder = new DeliverCommand(lastOrder);<br>            orderManager.addCommand(deliverOrder);<br>            orderManager.executeCommands();<br>        }<br>    }<br>    // ... other methods unchanged ...<br>}</pre><p>What we’ve just implemented follows a well-known software design pattern called the <strong>Command Pattern</strong>.</p><p><strong>Definition:</strong> The Command is a behavioral design pattern that encapsulates a request as an independent object, containing all the details needed to carry it out. This encapsulation allows you to treat requests as parameters for methods, delay or schedule their execution.</p><p>In this pattern:</p><ul><li><strong>Command</strong>: The OrderCommand interface and its implementations (SendToKitchenCommand, DeliverCommand, CancelCommand) define each action as an object, executed via execute().</li><li><strong>Invoker</strong>: The OrderManager class queues these command objects and triggers their execution, decoupling the Table from direct state changes.</li><li><strong>Receiver</strong>: The OrderItem class, with its existing Status enum and methods like sendToKitchen, receives and processes the commands, updating its state to reflect actions like preparation or delivery.</li></ul><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*BxitlowZrQkt3ygWHqtM8Q.png" /></figure><p>With our restaurant management system designed and implemented, let’s wrap up with key takeaways.</p><h3>Wrap Up</h3><p>In this chapter, we gathered requirements for the Restaurant Management System through a series of detailed questions and answers. We then identified the core objects involved, designed the class structure, and implemented the key components of the system.</p><p>A key takeaway from this design is the importance of modularity and adherence to the single responsibility principle. Each component, such as the Menu, ReservationManager, Layout, and Table classes, manages a distinct responsibility, ensuring the system remains maintainable and adaptable for future enhancements.</p><p>Our design choices, such as delegating operations in the Restaurant to act as a facade or using immutable MenuItem objects, prioritize flexibility and consistency. An alternative, like implementing reservation and order logic directly in the Restaurant class, may simplify the design but could increase complexity and reduce scalability by centralizing responsibilities. In an interview, revisiting these decisions and explaining their rationale demonstrates your ability to think critically about system design.</p><p>Congratulations on getting this far! Now give yourself a pat on the back. Good job!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba7b58286b5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object-Oriented Design Interview — ATM System]]></title>
            <link>https://medium.com/@dibasbehera/object-oriented-design-interview-atm-system-797a9eae2cc9?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/797a9eae2cc9</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Sat, 16 May 2026 18:46:00 GMT</pubDate>
            <atom:updated>2026-05-16T18:46:00.492Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design an ATM System</h3><p>In this chapter, we will explore the object-oriented design of an ATM system. The primary purpose of an ATM is to automate banking tasks for users, allowing them to check balances, withdraw cash, and transfer funds. This design aims to make these operations seamless by designing classes that model key components, such as the ATM machine, bank accounts, hardware interfaces, and transaction states.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/546/1*e_VSUlDKDfYuEzp9qBUWnw.png" /></figure><p>Let’s gather the specific requirements through a simulated interview scenario.</p><h3>Requirements Gathering</h3><p>Here is an example of a typical prompt an interviewer might give:</p><p>“Picture yourself approaching an ATM on a busy afternoon to manage your banking needs. You insert your card, enter your PIN, and then choose from options such as checking your balance, withdrawing cash, or depositing funds. Within seconds, the system verifies your credentials and processes your request. Behind the scenes, the ATM coordinates with the bank, manages account transactions, and interacts with hardware like the card reader and cash dispenser. Now, let’s design an ATM system that handles these operations smoothly and reliably.”</p><h3>Requirements clarification</h3><p>The first step in designing the ATM system is to understand precisely what the interviewer wants you to design. Here is an example of how a conversation between a candidate and an interviewer might unfold:</p><p><strong>Candidate</strong>: For user interactions, I think the ATM should have a card reader to process debit cards, a keypad for entering the PIN and selecting options, a screen to display instructions and menus, a cash dispenser for withdrawals, and a deposit slot for accepting cash. Does this cover the main components, or are there additional ones I should consider?<br><strong>Interviewer</strong>: That’s a solid list.</p><p><strong>Candidate:</strong> I envision the ATM guiding users through a clear flow: the user inserts their card, enters a PIN for authentication, and then sees a menu with options like checking balance, withdrawing cash, or depositing funds. After completing a task, the ATM offers the choice to continue or exit, ejecting the card at the end. Does this flow align with your vision, or should I adjust any steps?<br><strong>Interviewer:</strong> Your flow is accurate. It starts with card insertion, PIN entry, and a menu for tasks. After each task, the user can continue or exit, with the card ejected at the end.</p><p><strong>Candidate:</strong> For authentication, I assume the ATM validates the card and PIN combination. If either is invalid, it displays an error.<br><strong>Interviewer:</strong> Yes, the ATM validates the card and PIN, showing an error for invalid inputs. Including a limit of three PIN attempts before locking the card is a good security measure. Let’s keep that in scope.</p><p><strong>Candidate:</strong> Regarding accounts, I propose that the ATM supports multiple accounts per user, such as Checking and Savings, linked to their card. Users can select an account for transactions. Does this match your requirements?<br><strong>Interviewer:</strong> That’s correct. The ATM should support Checking and Savings accounts, with users able to select one for transactions. No additional account types are needed for now.</p><p><strong>Candidate:</strong> For transactions, I think the ATM should handle Withdraw and Deposit operations. For example, withdrawals check for sufficient funds, and deposits update the balance. Should we include other transactions like transfers, or focus on these two?<br><strong>Interviewer:</strong> Let’s focus on Withdraw and Deposit for simplicity. Transfers are out of scope for now. Ensure withdrawals validate funds and deposits process cash accurately.</p><p><strong>Candidate:</strong> To handle errors, I suggest the ATM displays clear messages for issues like insufficient funds, invalid PINs, or hardware failures.<br><strong>Interviewer:</strong> I agree that clear error messages are essential.</p><h3>Requirements</h3><p>Based on the requirements gathering dialogue, the following functional requirements are identified for the ATM system:</p><ul><li>Authenticate users via a debit card and PIN.</li><li>Support multiple accounts per user, including Checking and Savings account types, with the ability to select an account for transactions.</li><li>The machine should include a Card Reader, Keypad, Screen, Cash Dispenser, Deposit Slot, and an optional Printer for receipts.</li><li>Support Withdraw and Deposit transactions, ensuring withdrawals validate sufficient funds and deposits update the account balance.</li><li>Handle exceptions, such as insufficient funds or incorrect inputs, by displaying clear error messages and retaining the card after repeated invalid attempts.</li></ul><p>Below are the non-functional requirements:</p><ul><li>The ATM must protect user data with strong security measures and retain cards after repeated invalid PIN attempts to ensure user trust.</li><li>The ATM must operate reliably, minimizing disruptions and safely ejecting cards during failures to maintain user confidence.</li></ul><h3>Use Case Diagram</h3><p>In the ATM system, a use case diagram illustrates how customers interact with the system to perform banking tasks, clarifying essential actions such as card insertion, PIN entry with up to three attempts, account selection, transaction processing, and error handling.</p><p>Below is the use case diagram of the ATM system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/565/1*jwBSr7699neslbF0Ck8_dA.png" /></figure><p>The <strong>Customer</strong> actor has the following main use cases:</p><ul><li><strong>Insert Card:</strong> The customer initiates a session by inserting their card into the ATM.</li><li><strong>Enter PIN:</strong> After inserting the card, the customer then enters their PIN for authentication.</li><li><strong>Select Transaction:</strong> The customer chooses a transaction type, such as withdrawing cash or depositing funds, from the menu.</li><li><strong>Select Account:</strong> The customer selects an account (e.g., Checking or Savings) for the transaction.</li><li><strong>Withdraw Cash:</strong> The customer requests a cash withdrawal and specifies the desired amount.</li><li><strong>Deposit Funds:</strong> The customer deposits cash into the ATM.</li><li><strong>Eject Card:</strong> The customer requests card ejection (e.g., by canceling or ending the session), the ATM ejects the card, and the session terminates.</li></ul><p>The <strong>System</strong> actor’s use cases are listed below. Note that actors may not always be human.</p><ul><li><strong>Validate Card and PIN:</strong> The system verifies the customer’s card and PIN to grant access to banking options.</li><li><strong>Withdraw Cash:</strong> The system processes the withdrawal request, confirms sufficient funds, and dispenses cash.</li><li><strong>Deposit Funds:</strong> The system accepts the deposited cash and updates the customer’s account balance accordingly.</li><li><strong>Eject Card:</strong> The ATM ejects the card, and the session ends.</li></ul><h3>Identify Core Objects</h3><p>To design a modular and maintainable ATM system, we identify core objects that encapsulate distinct responsibilities, aligning with the functional requirements and user flow. These objects model the system’s key entities and interactions, ensuring clear separation of concerns and extensibility.</p><p><strong>Bank</strong>: Stores and manages accounts.</p><p><strong>Account:</strong> Represents a customer’s bank account. Manages a customer’s account details, including balance, account number, card number, PIN (hashed for security), and account type (Checking or Savings).</p><p><strong>ATM Machine</strong>: Acts as the main coordinator, managing user interaction and connecting with hardware elements like the card reader, keypad, screen, cash dispenser, and deposit slot.</p><p><strong>Design choice:</strong> We consolidate hardware access and management within the ATMMachine object to ensure consistent behavior across components, enabling a seamless user flow from card insertion to transaction completion.</p><p><strong>Transaction</strong>: Manages financial transactions like cash withdrawals and deposits, including validation checks (e.g., sufficient balance for withdrawals) and transaction execution.</p><h3>Design Class Diagram</h3><p>With the core objects and their roles defined, we now design their classes, attributes, and methods to construct a modular and extensible ATM system that meets the specified requirements.</p><h3>Account</h3><p>This class represents a customer’s bank account as a distinct entity within the ATM system, encapsulating the essential data and operations needed to support user authentication and financial transactions.</p><p>The Account class manages critical information, including the account balance, account number, associated card number, and PIN, while using an AccountType enum to distinguish between account types like Checking and Savings. It enables authentication by validating the PIN and supports transactions by updating the balance for withdrawals and deposits, ensuring the account’s state reflects the user’s financial activities accurately and efficiently.</p><p>Below are representations of those classes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*xwJJE2mDRF946Mu3iXwKlA.png" /></figure><p><strong>Important discussion:</strong> Rather than maintaining a transaction ledger for auditability and deriving the balance from it, as is typical in real-world banking, we simplify the system by directly updating the account balance during each transaction. This approach prioritizes the ATM’s core functionality and effective scope management.</p><h3>Bank</h3><p>In object-oriented design questions, systems are usually self-contained and don’t use real databases or web service API calls. The Bank class is designed to be a separate component that handles the main data and operations needed for the ATM to work.</p><p>The Bank class stores Account objects and links them to cards for fast retrieval, enabling efficient card and PIN validation for authentication, account access for transactions, and funds availability checks for withdrawals.</p><p>To ensure flexibility and scalability, we define a BankInterface to decouple the Bank implementation from the ATM. This allows the local Bank object to be easily replaced with a networked implementation, such as an API client adapter, if the system were to be extended for production use, without modifying the ATM’s core logic.</p><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/439/1*n7KBkOqgegs5VChwfQBCNw.png" /></figure><p><strong>Alternative approach:</strong> We could integrate the Bank’s functionality directly into the ATMMachine. However, this would tightly couple account management with the ATM’s operations, reducing modularity and making it harder to adapt the system for networked banking or other extensions in the future.</p><h3>Transaction</h3><p>The purpose of designing the Transaction class is to provide a unified framework for handling financial operations in the ATM system, enabling the system to process withdrawals and deposits consistently and reliably.</p><p>The Transaction class, as an interface, defines a contract for all transaction types, supported by a TransactionType enum that specifies Withdraw and Deposit operations. It validates transactions, such as checking funds for withdrawals, and updates the account balance, using concrete classes like WithdrawTransaction and DepositTransaction for reliable processing.</p><p><strong>Design choice:</strong> By abstracting Transaction as a separate object, we can represent different transaction types (e.g., Withdraw, Deposit) with shared behavior, making it easier to introduce new types in the future without modifying the core logic.</p><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*pVO_w9QYugHSLntNzxxjhw.png" /></figure><h3>ATM State</h3><p>The purpose of designing the ATMState interface is to provide a framework for handling the distinct stages of the ATM’s interaction process, ensuring the ATMMachine can manage each user step systematically.</p><p>​​​​The ATMState interface defines the operations for each stage of the ATM’s flow, such as card insertion, PIN entry, transaction selection, and cash handling. Concrete state classes like IdleState, PinEntryState, TransactionSelectionState, WithdrawAmountEntryState, and DepositCollectionState implement the specific behavior for each stage.</p><p>The following diagram shows the class hierarchy of ATMState and its concrete states:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*z-az6DWTYVJffzgswicsDg.png" /></figure><p>The following state transition diagram visualizes this design.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*NVwnoZgT9mfn3Jg3zU5_SQ.png" /></figure><ul><li>IdleState: Awaits card insertion, transitions to PinEntryState on valid card.</li><li>PinEntryState: Processes PIN entry, transitions to TransactionSelectionState on valid PIN.</li><li>TransactionSelectionState: Processes transaction selection, transitions to WithdrawAmountEntryState or DepositCollectionState.</li><li>WithdrawAmountEntryState: Handles withdrawal amount entry, executes transaction, returns to TransactionSelectionState or IdleState.</li><li>DepositCollectionState: Processes deposit amount, executes transaction, returns to TransactionSelectionState or IdleState.</li></ul><p><em>Note</em>: To learn more about the State pattern and its everyday use cases, refer to the <a href="https://bytebytego.com/courses/object-oriented-design-interview/design-a-vending-machine"><strong>Vending Machine</strong></a> chapter of the book.</p><h3>Hardware component interfaces</h3><p>The purpose of designing the hardware component interfaces is to provide a set of interfaces that handle user interactions and physical operations within the ATM system, ensuring the ATMMachine can operate independently of specific hardware implementations.</p><p>The hardware components, including CardProcessor, Keypad, Display, CashDispenser, and DepositBox, define the operations needed for interacting with the user and managing physical tasks, such as reading cards, accepting PIN entries, displaying messages, dispensing cash, and collecting deposits. They enable the ATMMachine to perform these tasks through well-defined interfaces, ensuring flexibility and ease of testing with simulated implementations.</p><h4>CardProcessor</h4><p>The CardProcessor interface enables the ATMMachine to read a card during insertion and release it after the user’s session, managing card-related operations for starting and ending transactions.</p><h4>Keypad</h4><p>The Keypad interface captures user inputs such as PINs, transaction choices, and amounts.</p><h4>DepositBox</h4><p>The DepositBox interface collects the deposited amount during a deposit transaction, enabling the ATMMachine to handle cash deposits from users.</p><h4>CashDispenser</h4><p>The CashDispenser interface delivers the requested cash to the user during a withdrawal transaction.</p><h4>Display</h4><p>The Display interface shows messages and prompts to guide the user.</p><p>​​The following diagram shows the hardware component interfaces and their relationship with the ATM Machine:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*cz2DkihO9WQCrDJMBzNFvg.png" /></figure><h3>ATM Machine</h3><p>The ATMMachine class acts as a facade for the ATM system, orchestrating user interactions, coordinating hardware components while relying on a Bank instance to access Account data and process Transactions. It uses the ATMState class to manage the sequence of steps in a user’s session, ensuring a clear and reliable experience from card insertion to session end.</p><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/487/1*iHcAqV9BBk6C_axgYC8tzA.png" /></figure><p><strong>Design choice:</strong> We employ the State pattern with ATMState classes to manage the ATM’s sequential workflow, such as transitioning from card insertion to PIN entry, ensuring each stage is encapsulated and clearly defined. Additionally, we use interface-based hardware components (e.g., CardProcessor, Keypad) to abstract physical interactions. This separation of concerns enhances modularity, simplifies maintenance, and enables testing with mock hardware implementations.</p><p>With all classes defined, let’s review how they fit together in the complete class diagram.</p><h3>Complete Class Diagram</h3><p>Take a moment to review the complete class structure and the relationships between them. The detailed methods and attributes are skipped to make the diagram more readable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/515/1*1g1xb0fY58tQcns352W33g.png" /></figure><h3>Code — ATM System</h3><p>In this section, we will implement the core functionalities of the ATM System, focusing on key areas such as defining different account types and transactions, managing accounts and banking operations, handling user interactions through hardware components, and implementing the ATM’s interaction flow using a state machine.</p><h3>Account</h3><p>The Account class represents a bank account in our ATM system, encapsulating core banking attributes like balance, account number, card details, and account type. It maintains security through PIN hashing and is designed with a mix of immutable (accountNumber, cardNumber, accountType, and cardPinHash) and mutable (balance) fields. This ensures account identity remains constant while allowing balance updates for deposits and withdrawals.</p><p>Tied to this, the AccountType enum defines the type of account a customer can hold, currently supporting CHECKING and SAVING types. This simple enum design enables type-safe account categorization while allowing for future expansion of account types.</p><p>Below is the code implementation of the Account class and AccountType enum:</p><pre>// Represents a bank account with balance, card details, and PIN security<br>public class Account {<br><br>    private BigDecimal balance;<br>    private final String accountNumber;<br>    private final String cardNumber;<br>    private final byte[] cardPinHash;<br>    private final AccountType accountType;<br><br>    // Creates a new account with initial zero balance and hashed PIN<br>    public Account(<br>            final String accountNumber,<br>            final AccountType type,<br>            final String cardNumber,<br>            final String pin) {<br>        this.accountNumber = accountNumber;<br>        this.accountType = type;<br>        this.cardNumber = cardNumber;<br>        this.cardPinHash = calculateMd5(pin); // PIN is hashed for security<br>        this.balance = BigDecimal.ZERO;<br>    }<br><br>    // Validates the entered PIN against stored hash<br>    public boolean validatePin(String pinNumber) {<br>        byte[] entryPinHash = calculateMd5(pinNumber);<br>        return Arrays.equals(cardPinHash, entryPinHash);<br>    }<br><br>    // Updates account balance by adding the specified amount<br>    public void updateBalanceWithTransaction(final BigDecimal balanceChange) {<br>        this.balance = this.balance.add(balanceChange);<br>    }<br><br>    // getter methods omitted for brevity<br><br>}<br><br>// Defines the type of bank account<br>public enum AccountType {<br>    // Regular checking account for daily transactions<br>    CHECKING,<br>    // Interest-bearing savings account<br>    SAVING<br>}</pre><ul><li>The validatePin method ensures secure authentication by comparing the hashed input PIN with the stored hash.</li><li>The updateBalanceWithTransaction handles both deposits and withdrawals through a single method that adds the transaction amount (positive for deposits, negative for withdrawals) to the current balance.</li></ul><p><strong>Implementation choice:</strong> We have used BigDecimal for balance to ensure precise financial calculations, while the immutable fields for account identity (accountNumber, cardNumber) maintain data consistency throughout the account’s lifecycle.</p><h3>Bank</h3><p>The BankInterface defines the contract for banking operations in the ATM system. Its concrete implementation, the Bank class, provides a local implementation to manage accounts and handle operations like card validation and fund withdrawals. This separation allows the system to remain flexible, supporting potential future extensions such as replacing the Bank with a networked implementation.</p><p>Here’s how it’s structured:</p><pre>public interface BankInterface {<br>    void addAccount(String accountNumber, AccountType type, String cardNumber, String pin);<br><br>    boolean validateCard(String cardNumber);<br><br>    boolean checkPin(String cardNumber, String pinNumber);<br><br>    Account getAccountByAccountNumber(String accountNumber);<br><br>    Account getAccountByCard(String cardNumber);<br><br>    boolean withdrawFunds(Account account, BigDecimal amount);<br>}<br><br>// Manages bank accounts and provides banking operations like validation and transactions<br>public class Bank implements BankInterface {<br>    private final Map&lt;String, Account&gt; accounts = new HashMap&lt;&gt;();<br>    private final Map&lt;String, Account&gt; accountByCard = new HashMap&lt;&gt;();<br><br>    // Creates a new account and stores it in both account and card maps<br>    @Override<br>    public void addAccount(<br>            final String accountNumber,<br>            final AccountType type,<br>            final String cardNumber,<br>            final String pin) {<br>        final Account newAccount = new Account(accountNumber, type, cardNumber, pin);<br>        accounts.put(newAccount.getAccountNumber(), newAccount);<br>        accountByCard.put(newAccount.getCardNumber(), newAccount);<br>    }<br><br>    // Checks if a card number exists in the bank&#39;s records<br>    @Override<br>    public boolean validateCard(final String cardNumber) {<br>        return getAccountByCard(cardNumber) != null;<br>    }<br><br>    // Verifies if the provided PIN matches the card&#39;s stored PIN<br>    @Override<br>    public boolean checkPin(String cardNumber, String pinNumber) {<br>        Account account = getAccountByCard(cardNumber);<br>        if (account != null) {<br>            return account.validatePin(pinNumber);<br>        }<br>        return false;<br>    }<br><br>    // Retrieves account by account number<br>    @Override<br>    public Account getAccountByAccountNumber(String accountNumber) {<br>        return accounts.get(accountNumber);<br>    }<br><br>    // Retrieves account by card number<br>    @Override<br>    public Account getAccountByCard(String cardNumber) {<br>        return accountByCard.get(cardNumber);<br>    }<br><br>    // Attempts to withdraw specified amount from account if sufficient funds exist<br>    @Override<br>    public boolean withdrawFunds(Account account, BigDecimal amount) {<br>        if (account.getBalance().compareTo(amount) &gt;= 0) {<br>            account.updateBalanceWithTransaction(amount.negate());<br>            return true;<br>        }<br>        return false;<br>    }<br>}</pre><p>The Bank class implements efficient account management through:</p><p><strong>Two HashMaps for O(1) lookup time:</strong></p><ul><li>accounts: Maps account numbers to Account objects</li><li>accountByCard: Maps card numbers to Account objects for quick access by card number during authentication and transactions.</li></ul><p><strong>Account operations:</strong></p><ul><li>addAccount: Creates new accounts.</li><li>validateCard: Checks if the card number is associated with an account, used during card insertion.</li><li>checkPin: Verifies the PIN for the given card, enabling user authentication.</li><li>withdrawFunds: Processes withdrawals with balance checks.</li></ul><p><strong>Implementation choice:</strong> We chose HashMap for both accounts (mapping account numbers to Account objects) and accountByCard (mapping card numbers to Account objects) because it provides average-case O(1) time complexity for lookups, ensuring fast performance for real-time operations like card validation and account retrieval during a user session.</p><h3>Transaction</h3><p>The Transaction interface establishes a consistent framework for handling all financial transactions within the ATM system. It ensures that different transaction types, such as withdrawals and deposits, adhere to a uniform process for validation and execution. The WithdrawTransaction and DepositTransaction classes implement this interface to manage their respective operations. This abstraction promotes extensibility, making it easy to introduce new transaction types, like transfers, without altering existing code.</p><p>Here is the implementation of the interface and its concrete classes.</p><pre>public interface Transaction {<br>    TransactionType getType();<br><br>    boolean validateTransaction();<br><br>    void executeTransaction();<br>}<br><br>// Handles the withdrawal transaction process for removing funds from an account<br>public class WithdrawTransaction implements Transaction {<br>    Account account;<br>    BigDecimal amount;<br><br>    // Returns the transaction type as WITHDRAW<br>    @Override<br>    public TransactionType getType() {<br>        return TransactionType.WITHDRAW;<br>    }<br><br>    // Validates if the account has sufficient funds for withdrawal<br>    @Override<br>    public boolean validateTransaction() {<br>        assert account != null;<br>        return account.getBalance().compareTo(amount) &gt; 0;<br>    }<br><br>    // Creates a new withdrawal transaction, throws exception if validation fails<br>    public WithdrawTransaction(Account account, BigDecimal amount) {<br>        if (!validateTransaction()) {<br>            throw new IllegalStateException(<br>                    &quot;Cannot complete withdrawal: Insufficient funds in account&quot;);<br>        }<br>        this.account = account;<br>        this.amount = amount;<br>    }<br><br>    // Executes the withdrawal by subtracting the amount from account balance<br>    @Override<br>    public void executeTransaction() {<br>        account.updateBalanceWithTransaction(amount.negate());<br>    }<br>}<br><br>// Handles the deposit transaction process for adding funds to an account<br>public class DepositTransaction implements Transaction {<br>    final Account account;<br>    final BigDecimal amount;<br><br>    // Returns the transaction type as DEPOSIT<br>    @Override<br>    public TransactionType getType() {<br>        return TransactionType.DEPOSIT;<br>    }<br><br>    // Deposit transactions are always valid<br>    @Override<br>    public boolean validateTransaction() {<br>        return true;<br>    }<br><br>    public DepositTransaction(Account account, BigDecimal amount) {<br>        this.account = account;<br>        this.amount = amount;<br>    }<br><br>    // Executes the deposit by adding the amount to the account balance<br>    @Override<br>    public void executeTransaction() {<br>        account.updateBalanceWithTransaction(amount);<br>    }<br>}</pre><p>The Transaction interface includes essential methods to manage transactions:</p><ul><li>getType(): Returns the transaction type (e.g., WITHDRAW or DEPOSIT).</li><li>validateTransaction(): Validates the transaction’s feasibility, such as checking funds availability for withdrawals.</li><li>executeTransaction(): Executes transaction by updating the account balance.</li></ul><h3>ATM State</h3><p>The ATMState abstract class forms the foundation of our ATM’s state machine, ensuring that each stage of the user flow, from card insertion to transaction completion, follows a consistent process. Its seven concrete implementations manage distinct stages, enabling the ATMMachine to execute the user flow by transitioning between states as the user progresses through their session.</p><p>The ATMState interface includes essential methods to manage each stage:</p><ul><li>processCardInsertion(String cardNumber): Handles card insertion, used at the start of a session.</li><li>processCardEjection(): Handles card ejection, which is used to end a session.</li><li>processPinEntry(String pinNumber): Processes PIN entry for authentication.</li><li>processWithdrawalRequest(): Initiates a withdrawal transaction.</li><li>processDepositRequest(): Initiates a deposit transaction.</li><li>processAmountEntry(BigDecimal amount): Processes the amount for withdrawals.</li><li>processDepositCollection(BigDecimal amount): Processes the amount for deposits.</li></ul><p>Below is the implementation for the ATMState interface, showing its default behavior, followed by implementations of IdleState and WithdrawAmountEntryState:</p><pre>public class ATMState {<br>    // Displays an invalid action message on the ATM screen<br>    private static void renderDefaultAction(ATMMachine atmMachine) {<br>        atmMachine.getDisplay().showMessage(&quot;Invalid action, please try again.&quot;);<br>    }<br><br>    // Default implementation for card insertion<br>    public void processCardInsertion(ATMMachine atmMachine, String cardNumber) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for card ejection<br>    public void processCardEjection(ATMMachine atmMachine) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for PIN entry<br>    public void processPinEntry(ATMMachine atmMachine, String pin) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for withdrawal request<br>    public void processWithdrawalRequest(ATMMachine atmMachine) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for deposit request<br>    public void processDepositRequest(ATMMachine atmMachine) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for amount entry<br>    public void processAmountEntry(ATMMachine atmMachine, BigDecimal amount) {<br>        renderDefaultAction(atmMachine);<br>    }<br><br>    // Default implementation for deposit collection<br>    public void processDepositCollection(ATMMachine atmMachine, BigDecimal amount) {<br>        renderDefaultAction(atmMachine);<br>    }<br>}</pre><p>The IdleState waits for the user to insert a card and start the session, supporting only card insertion. If the card is valid, it transitions to PinEntryState. Otherwise, it displays an error.</p><pre>public class IdleState extends ATMState {<br>    /**<br>     * This method is called when a card is inserted into the ATM. This transitions the ATM to the<br>     * PinEntryState if the card is valid.<br>     */<br>    @Override<br>    public void processCardInsertion(ATMMachine atmMachine, String cardNumber) {<br>        if (atmMachine.getBankInterface().validateCard(cardNumber)) {<br>            atmMachine.getDisplay().showMessage(&quot;Please enter your PIN&quot;);<br>            atmMachine.transitionToState(new PinEntryState());<br>        } else {<br>            atmMachine.getDisplay().showMessage(&quot;Invalid card. Please try again.&quot;);<br>        }<br>    }<br>}</pre><p>The EnterWithdrawalAmountState manages the step where the user enters a withdrawal amount, coordinating with the ATMMachine to execute the withdrawal via the Bank, using a WithdrawTransaction to validate funds and update the account balance.</p><pre>public class WithdrawAmountEntryState extends ATMState {<br>    // Handles card ejection by canceling transaction and returning to idle state<br>    @Override<br>    public void processCardEjection(ATMMachine atmMachine) {<br>        atmMachine.getDisplay().showMessage(&quot;Transaction cancelled, card ejected&quot;);<br>        atmMachine.transitionToState(new IdleState());<br>    }<br><br>    // Processes withdrawal request by checking balance and dispensing cash if sufficient funds<br>    @Override<br>    public void processAmountEntry(ATMMachine atmMachine, BigDecimal amount) {<br>        String cardNumber = atmMachine.getCardProcessor().getCardNumber();<br>        Account account = atmMachine.getBankInterface().getAccountByCard(cardNumber);<br>        boolean isSuccess = atmMachine.getBankInterface().withdrawFunds(account, amount);<br><br>        if (isSuccess) {<br>            atmMachine.getCashDispenser().dispenseCash(amount);<br>            atmMachine.getDisplay().showMessage(&quot;Please take your cash.&quot;);<br>        } else {<br>            atmMachine.getDisplay().showMessage(&quot;Insufficient funds, please try again.&quot;);<br>        }<br>        atmMachine.transitionToState(new TransactionSelectionState());<br>    }<br>}</pre><p>For brevity, we have omitted the code for the PinEntryState, TransactionSelectionState, and DepositCollectionState classes. The complete code for all state classes is available in the accompanying materials of this book.</p><h3>ATMMachine</h3><p>The ATMMachine class acts as the central controller of the ATM system, applying the Facade pattern to offer a simplified interface to the underlying complexities. It manages the ATM’s hardware components, delegates state-specific behavior to corresponding ATMState classes such as IdleState and DepositCollectionState, and utilizes the BankInterface to perform banking operations.</p><p>Here is the implementation of this class.</p><pre>// Main ATM machine class that manages the state and hardware components of the ATM<br>public class ATMMachine {<br>    private ATMState state;<br><br>    private final CardProcessor cardProcessor;<br>    private final DepositBox depositBox;<br>    private final CashDispenser cashDispenser;<br>    private final Keypad keypad;<br>    private final Display display;<br><br>    private final Bank bank;<br><br>    // Initializes ATM with all required hardware components and bank interface<br>    public ATMMachine(<br>            Bank bank,<br>            CardProcessor cardProcessor,<br>            DepositBox depositBox,<br>            CashDispenser cashDispenser,<br>            Keypad keypad,<br>            Display display) {<br>        this.bank = bank;<br>        this.cardProcessor = cardProcessor;<br>        this.depositBox = depositBox;<br>        this.cashDispenser = cashDispenser;<br>        this.keypad = keypad;<br>        this.display = display;<br>        this.state = new IdleState();<br>    }<br><br>    // Forwards card insertion to current state for processing<br>    public void insertCard(String cardNumber) {<br>        state.processCardInsertion(this, cardNumber);<br>    }<br><br>    // Forwards card ejection to current state for processing<br>    public void ejectCard() {<br>        state.processCardEjection(this);<br>    }<br><br>    // Forwards PIN entry to current state for validation<br>    public void enterPin(String pin) {<br>        state.processPinEntry(this, pin);<br>    }<br><br>    // Forwards withdrawal request to current state for processing<br>    public void withdrawRequest() {<br>        state.processWithdrawalRequest(this);<br>    }<br><br>    // Forwards deposit request to current state for processing<br>    public void depositRequest() {<br>        state.processDepositRequest(this);<br>    }<br><br>    // Forwards amount entry to current state for processing<br>    public void enterAmount(BigDecimal amount) {<br>        state.processAmountEntry(this, amount);<br>    }<br><br>    // Forwards deposit collection to current state for processing<br>    public void collectDeposit(BigDecimal amount) {<br>        state.processDepositCollection(this, amount);<br>    }<br><br>    // Returns the display component for showing messages<br>    public Display getDisplay() {<br>        return display;<br>    }<br><br>    // Returns the cash dispenser component for handling withdrawals<br>    public CashDispenser getCashDispenser() {<br>        return cashDispenser;<br>    }<br><br>    // Returns the bank interface for account operations<br>    public BankInterface getBankInterface() {<br>        return bank;<br>    }<br><br>    // Returns the card processor component for handling card operations<br>    public CardProcessor getCardProcessor() {<br>        return cardProcessor;<br>    }<br><br>    // Returns the keypad component for user input<br>    public Keypad getKeypad() {<br>        return keypad;<br>    }<br><br>    // Updates the current state of the ATM<br>    public void transitionToState(ATMState nextState) {<br>        this.state = nextState;<br>    }<br><br>    // Returns the current state of the ATM<br>    public ATMState getCurrentState() {<br>        return state;<br>    }<br><br>    // Returns the deposit box component for handling deposits<br>    public DepositBox getDepositBox() {<br>        return depositBox;<br>    }<br>}</pre><p><strong>Hardware Management</strong>:</p><ul><li>The ATMMachine class manages all hardware components, including the card processor, cash dispenser, deposit box, keypad, and display, to enable user interactions.</li><li>It provides access to these hardware components through getter methods like getCardProcessor and getDisplay for use by state classes.</li></ul><p><strong>State Management</strong>:</p><ul><li>The ATMMachine class maintains the current state of the ATM using a state field to track the active stage of the user flow.</li><li>It provides state transition functionality through the setState method, allowing the ATM to move between stages like IdleState and PinEntryState.</li></ul><h3>Wrap Up</h3><p>With the ATM system fully implemented, it’s time to step back and consider what we’ve achieved. This chapter began by gathering requirements through a structured dialogue, then progressed to defining core objects like accounts and transactions, designing their class structure, and coding the essential components, including the state machine and hardware interactions.</p><p>The system’s maintainability and extensibility are ensured by the clear division of responsibilities among the classes: Account and Bank manage account data and banking operations, Transaction handles financial operations, ATMState and its state classes (IdleState, PinEntryState, etc.) manage the user flow stages, hardware interfaces (Keypad, CardProcessor, etc.) handle user interactions, and ATMMachine orchestrates the entire flow as a facade. Our choices, such as using the state pattern with ATMState and separating hardware interactions into interfaces, improve modularity and allow future extensions, like adding new transaction types or hardware components.</p><p>Congratulations on getting this far! Now give yourself a pat on the back. Good job!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=797a9eae2cc9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object-Oriented Design Interview — Shipping Locker System]]></title>
            <link>https://medium.com/@dibasbehera/object-oriented-design-interview-shipping-locker-system-aacbac902f78?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/aacbac902f78</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Fri, 15 May 2026 18:41:00 GMT</pubDate>
            <atom:updated>2026-05-15T18:41:00.580Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design a Shipping Locker System</h3><p>In this chapter, we will design a Shipping Locker system similar to UPS, FedEx, or Amazon Locker. It offers customers a convenient and secure way to pick up their online orders. The system manages locker availability, assigns incoming packages to appropriate lockers, and ensures a smooth package retrieval process for customers.</p><p>To realize this vision of a convenient and secure locker system, let’s explore what it needs to do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/538/1*WZBH5SEHVKfN6U3k0zMknQ.png" /></figure><h3>Requirements Gathering</h3><p>The first step in designing a shipping locker system is to clarify the requirements and narrow down the scope. Here is an example of a typical prompt an interviewer might give:</p><p>“Imagine you’ve just received a notification that your online order has been delivered to a shipping locker near you. You head to the locker, enter a secure access code, and instantly, the door pops open, revealing your package. Behind the scenes, the system has already managed locker availability, assigned an appropriately sized compartment, and ensured a seamless pickup experience. Now, let’s design a shipping locker system that can do all of this.”</p><h3>Requirements clarification</h3><p>Here is an example of how a conversation between a candidate and an interviewer might unfold:</p><p><strong>Candidate:</strong> Does the system support multiple locker sizes?<br><strong>Interviewer:</strong> Yes, the system has lockers of different sizes. The design should ensure packages are assigned to the smallest available locker that fits, optimizing space use.</p><p><strong>Candidate:</strong> So, what happens when a package gets to the locker, and how does the customer end up picking it up?<br><strong>Interviewer:</strong> When a package arrives, the system finds an open locker that’s the right size and assigns it there. Once the package is tucked inside, the customer gets a notification with the locker’s location and a unique access code. They just punch in the code to pop the locker open, grab their package, and that frees up the locker for the next delivery.</p><p><strong>Candidate:</strong> Is there a time limit or any fees for using the locker?<br><strong>Interviewer:</strong> Good question. That’s actually a big part of how the system works. We’ve got a locker policy that gives customers a ‘Free Period’, a set number of days during which they can use the locker for free. Once that’s up, we start charging a daily fee based on the locker’s size. And if the package sits there past a ‘Maximum Period,’ which is also predefined, one of our staff members steps in, pulls it out, and clears the locker for someone else.</p><p><strong>Candidate:</strong> Since the system tracks locker usage costs through the locker policy, does it handle payments too?<br><strong>Interviewer:</strong> For simplicity’s sake, we let an external service take care of the actual payment processing. That’s not something the system itself has to worry about.</p><h3>Requirements</h3><p>Here are the key functional requirements we’ve identified.</p><ul><li>The system should keep track of all lockers and support different locker sizes.</li><li>The system should smartly assign lockers by matching package size to the smallest available option that fits, keeping things efficient.</li><li>Customers should be able to pop open their assigned locker with a unique access code.</li><li>The system should monitor storage costs based on the customer’s locker policy, factoring in the daily rate tied to that specific locker size.</li></ul><p>Below are the non-functional requirements:</p><ul><li>The system should handle a high volume of locker operations per site without performance degradation, accommodating busy locations.</li><li>The system must maintain high availability, ensuring lockers are accessible to customers and staff at all times.</li></ul><p>Now that we’ve outlined the system’s needs, let’s pinpoint the building blocks to fulfill them.</p><h3>Identify Core Objects</h3><p>Before diving into the design, it’s important to identify the core objects.</p><ul><li><strong>Locker:</strong> This class represents an individual locker.</li><li><strong>Site:</strong> This class represents a locker facility that consists of multiple lockers of various sizes. It is responsible for managing the collection of lockers and organizing them by size.</li><li><strong>ShippingPackage:</strong> An interface defining the standard for packages, with BasicShippingPackage as its concrete implementation, tracking details like order ID, dimensions, and status.</li><li><strong>Account:</strong> This class represents customers and their associated accounts. Customers own packages stored in lockers, and their accounts store policy information for free and maximum storage periods, along with their current balance.</li></ul><h3>Design Class Diagram</h3><p>Now that we know the core objects and their roles, the next step is to create classes and methods that turn the requirements into an easy-to-maintain system. Let’s take a closer look.</p><h3>Locker</h3><p>The Locker class represents a physical storage unit for holding packages. It includes the following attributes:</p><ul><li><strong>LockerSize size:</strong> Represents the size of the locker.</li><li><strong>ShippingPackage currentPackage:</strong> Stores the package currently assigned to the locker.</li><li><strong>Date assignmentDate:</strong> Tracks the date when the package was placed in the locker.</li><li><strong>String accessCode:</strong> A unique security code required for package retrieval.</li></ul><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*1QoCCSm2qG_GGJukr95HDA.png" /></figure><p>It provides functionalities such as assigning a package to the locker, releasing the locker upon package retrieval, calculating storage charges based on usage duration, determining locker availability, and ensuring secure access through code verification.</p><p><strong>Design choice:</strong> The Locker class is designed as a standalone entity to encapsulate the state and behavior of an individual locker, ensuring modularity and ease of maintenance.</p><h3>LockerSize</h3><p>The LockerSize enum represents the predefined sizes of lockers available in the system. Each size is associated with specific attributes that define its dimensions and daily usage charge.</p><ul><li><strong>String sizeName</strong>: A label identifying the locker size (e.g., ‘Small,’ ‘Medium,’ ‘Large’).</li><li><strong>BigDecimal dailyCharge</strong>: The cost per day for using a locker of a specific size (“Small,” “Medium,” or “Large”).</li><li><strong>BigDecimal width, height, depth</strong>: The physical dimensions of the locker, determining its capacity to accommodate different package sizes.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*GIfvaBnzoWyAOrTkMUAIJQ.png" /></figure><p><strong>Design choice:</strong> We use an enum here because locker sizes, such as Small, Medium, and Large, are a fixed set of options that don’t change during runtime, ensuring type safety and simplicity.</p><h3>Site</h3><p>The Site class models a physical location containing a collection of lockers, organized by their size.</p><p>Key functionalities include:</p><ul><li><strong>Locker findAvailableLocker(LockerSize size):</strong> This method searches for an empty locker that matches the exact size you need, like “small,” “medium,” or “large.”</li><li><strong>Locker placePackage(ShippingPackage pkg, Date date):</strong> This method takes a package, finds it a locker that fits, and locks it inside. It also keeps track of when the package was placed there and updates the package’s status.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*D-5q3VLBnU4fQlbIZaFnmA.png" /></figure><p>The lockers at each site are managed using a map-based structure, where the key is a LockerSize and the value is a set of lockers for that size. This structure allows quick access to available lockers based on their size.</p><h3>ShippingPackage</h3><p>We have modeled ShippingPackage as an interface to establish a standard for all package types within the locker system. It defines key methods that any package type must implement to ensure compatibility with locker storage and retrieval processes. The BasicShippingPackage class is a concrete implementation of the ShippingPackage interface. It represents a standard package intended for storage in a locker.</p><p>The ShippingStatus enum defines a fixed set of valid states for a package’s lifecycle in the locker system, such as PENDING, STORED, and RETRIEVED. This enum ensures that package status updates are consistent and restricted to predefined values, enhancing type safety.</p><p>The UML diagram below illustrates this structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/539/1*eBJPlysoHYotYuKKJ5kgLw.png" /></figure><p><strong>Design choice:</strong> Modeling ShippingPackage as an interface allows for extensibility, enabling the system to support diverse package types (e.g., fragile or perishable) without modifying core logic.</p><h3>Account</h3><p>The Account class represents a customer using the locker system. It maintains the customer’s balance for locker-related charges and is associated with an AccountLockerPolicy, which defines terms for free usage limits and maximum storage duration. This ensures that charges are applied in accordance with the policy.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*FIPJcQ9Y3d0bhqorCvCcDA.png" /></figure><p>The class provides functionality to add funds to the account.</p><p><strong>Design choice:</strong> By associating an AccountLockerPolicy, it supports flexible billing rules based on customer policies. This separation of customer and policy data enhances maintainability and allows for personalized locker usage terms.</p><h3>AccountLockerPolicy</h3><p>This class defines the rules and policies for locker usage associated with an account. It includes the number of days during which locker usage is free (freePeriodDays) and the maximum number of days a package can remain in the locker before it must be cleared (maximumPeriodDays). Here is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*UTWNp_ayE-v5HeozCdP38A.png" /></figure><h3>NotificationInterface</h3><p>Defines the contract for sending notifications. It includes a single method, sendNotification, which takes a message and an account as parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*7f5MyOnGZP8iPXm6M6j0ug.png" /></figure><p>Implementations of this interface manage user notifications, such as alerts for package pickup availability and locker usage fees.</p><p><strong>Best practices:</strong> In OOD interviews, external systems like notifications are often represented as interfaces to keep the design flexible and scalable while avoiding unnecessary complexity.</p><h3>LockerManager</h3><p>The LockerManager class is responsible for managing package storage and retrieval at a specific site, ensuring that packages are assigned to suitable lockers based on size availability. It works with the following components:</p><ul><li><strong>Site site:</strong> Represents the physical location of the lockers.</li><li><strong>NotificationInterface notificationService:</strong> Sends notifications to customers when their packages are assigned to a locker or ready for pick-up.</li><li><strong>Map&lt;String, Account&gt; accounts</strong>: Maintains a mapping of account IDs to user accounts for managing locker usage and charges.</li><li><strong>Map&lt;String, Locker&gt; accessCodeMap:</strong> Maintains a mapping of access codes to lockers, allowing for quick retrieval during package pick-up.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*dyYMwksYKxtj8yzBaJsGNQ.png" /></figure><p><strong>Design choice:</strong> The LockerManager class is designed as a facade to simplify interactions between core objects, providing a single point of control for package assignment and retrieval.</p><h3>Complete Class Diagram</h3><p>Below is the complete class diagram of the shipping locker service:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*WxlWIhLz1l_EYUxhO3NNoQ.png" /></figure><p>With the class diagram as our guide, let’s bring the system to life through code.</p><h3>Code — Shipping Locker Service</h3><p>After completing the class diagram, the interviewer might ask you to implement key components of the shipping locker system. In this section, we’ll implement essential classes that handle locker operations, package assignments, and notifications.</p><h3>Locker and LockerSize</h3><p>The Locker class represents an individual locker unit. Each locker has a fixed size (an instance of LockerSize) and maintains details about the currently stored package, including the assignment date and a randomly generated access code for secure retrieval.</p><p>This class provides functionalities for:</p><ul><li>Assigning a package to a locker, recording the assignment date, and generating a secure access code.</li><li>Calculating storage charges based on the number of days the package has been stored and the locker’s daily rate, according to its size.</li><li>Once the package is picked up, make the locker available for future use.</li></ul><p>Below is the implementation of this class.</p><pre>public class Locker {<br>    // Size of the locker<br>    private final LockerSize size;<br>    // Currently stored package<br>    private ShippingPackage currentPackage;<br>    // Date when the current package was assigned<br>    private Date assignmentDate;<br>    // Access code for retrieving the package<br>    private String accessCode;<br><br>    public Locker(LockerSize size) {<br>        this.size = size;<br>    }<br><br>    // Assigns a package to this locker and generates an access code<br>    public void assignPackage(ShippingPackage pkg, Date date) {<br>        this.currentPackage = pkg;<br>        this.assignmentDate = date;<br>        this.accessCode = generateAccessCode();<br>    }<br><br>    // Releases the locker by removing the current package and its details<br>    public void releaseLocker() {<br>        this.currentPackage = null;<br>        this.assignmentDate = null;<br>        this.accessCode = null;<br>    }<br><br>    // Calculates storage charges based on usage duration and policy<br>    public BigDecimal calculateStorageCharges() {<br>        if (currentPackage == null || assignmentDate == null) {<br>            return BigDecimal.ZERO;<br>        }<br><br>        AccountLockerPolicy policy = currentPackage.getUser().getLockerPolicy();<br>        long totalDaysUsed =<br>                (new Date().getTime() - assignmentDate.getTime()) / (1000 * 60 * 60 * 24);<br><br>        // Check if exceeds maximum period<br>        if (totalDaysUsed &gt; policy.getMaximumPeriodDays()) {<br>            currentPackage.updateShippingStatus(ShippingStatus.EXPIRED);<br>            throw new MaximumStoragePeriodExceededException(<br>                    &quot;Package has exceeded maximum allowed storage period of &quot;<br>                            + policy.getMaximumPeriodDays()<br>                            + &quot; days&quot;);<br>        }<br><br>        // Calculate chargeable days (excluding free period)<br>        long chargeableDays = Math.max(0, totalDaysUsed - policy.getFreePeriodDays());<br>        return size.dailyCharge.multiply(new BigDecimal(chargeableDays));<br>    }<br><br>    // Checks if the locker is available for new packages<br>    public boolean isAvailable() {<br>        return currentPackage == null;<br>    }<br><br>    // Verifies if the provided access code matches the locker&#39;s code<br>    public boolean checkAccessCode(String code) {<br>        return this.accessCode != null &amp;&amp; accessCode.equals(code);<br>    }<br><br>    // getter and setter methods are omitted for brevity<br>}</pre><p>The LockerSize enum defines different locker sizes, each with an associated daily charge rate and physical dimensions (width, height, and depth). These predefined sizes allow the system to categorize lockers and ensure packages are placed in appropriately sized compartments.</p><pre>public enum LockerSize {<br>    // Small locker with 10x10x10 dimensions and $5 daily charge<br>    SMALL(<br>            &quot;Small&quot;,<br>            new BigDecimal(&quot;5.00&quot;),<br>            new BigDecimal(&quot;10.00&quot;),<br>            new BigDecimal(&quot;10.00&quot;),<br>            new BigDecimal(&quot;10.00&quot;)),<br>    // Medium locker with 20x20x20 dimensions and $10 daily charge<br>    MEDIUM(<br>            &quot;Medium&quot;,<br>            new BigDecimal(&quot;10.00&quot;),<br>            new BigDecimal(&quot;20.00&quot;),<br>            new BigDecimal(&quot;20.00&quot;),<br>            new BigDecimal(&quot;20.00&quot;)),<br>    // Large locker with 30x30x30 dimensions and $15 daily charge<br>    LARGE(<br>            &quot;Large&quot;,<br>            new BigDecimal(&quot;15.00&quot;),<br>            new BigDecimal(&quot;30.00&quot;),<br>            new BigDecimal(&quot;30.00&quot;),<br>            new BigDecimal(&quot;30.00&quot;));<br><br>    // Name of the locker size<br>    final String sizeName;<br>    // Daily charge for using this size locker<br>    final BigDecimal dailyCharge;<br>    // Width of the locker in inches<br>    final BigDecimal width;<br>    // Height of the locker in inches<br>    final BigDecimal height;<br>    // Depth of the locker in inches<br>    final BigDecimal depth;<br><br>    // Creates a new locker size with specified dimensions and charges<br>    LockerSize(<br>            String sizeName,<br>            BigDecimal dailyCharge,<br>            BigDecimal width,<br>            BigDecimal height,<br>            BigDecimal depth) {<br>        this.sizeName = sizeName;<br>        this.dailyCharge = dailyCharge;<br>        this.width = width;<br>        this.height = height;<br>        this.depth = depth;<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><p><strong>Implementation choice:</strong> You might have noticed that we have used BigDecimal for dailyCharge and dimensions in the LockerSize enum to ensure precision in financial calculations, unlike float or double, which could introduce rounding errors.</p><h3>Site</h3><p>The Site class represents a physical location containing multiple lockers, organized by size for efficient storage management. It maintains a collection of lockers grouped by size, treating lockers of the same size as interchangeable. This means any available locker of a given size can store a package, simplifying locker assignment.</p><p>To efficiently manage lockers, the class uses a map-based structure, where:</p><ul><li>Each key is a LockerSize, representing a specific locker category (“Small,” “Medium,” “Large”).</li><li>Each value is a Set&lt;Locker&gt;, containing all lockers of that size.</li></ul><p>This design allows the system to quickly find an available locker and assign packages accordingly.</p><p>Here is the implementation of this class.</p><pre>public class Site {<br>    // Map of locker sizes to sets of lockers of that size<br>    final Map&lt;LockerSize, Set&lt;Locker&gt;&gt; lockers = new HashMap&lt;&gt;();<br><br>    // Creates a new site with specified number of lockers for each size<br>    public Site(Map&lt;LockerSize, Integer&gt; lockers) {<br>        for (Map.Entry&lt;LockerSize, Integer&gt; entry : lockers.entrySet()) {<br>            Set&lt;Locker&gt; lockerSet = new HashSet&lt;&gt;();<br>            for (int i = 0; i &lt; entry.getValue(); i++) {<br>                lockerSet.add(new Locker(entry.getKey()));<br>            }<br>            this.lockers.put(entry.getKey(), lockerSet);<br>        }<br>    }<br><br>    // Finds an available locker of the specified size<br>    public Locker findAvailableLocker(LockerSize size) {<br>        for (Locker locker : lockers.get(size)) {<br>            if (locker.isAvailable()) {<br>                return locker;<br>            }<br>        }<br>        return null;<br>    }<br><br>    // Places a package in an available locker of appropriate size<br>    public Locker placePackage(ShippingPackage pkg, Date date) {<br>        // Determine the smallest locker size that can fit this package<br>        LockerSize size = pkg.getLockerSize();<br>        Locker locker = findAvailableLocker(size);<br>        if (locker != null) {<br>            locker.assignPackage(pkg, date);<br>            pkg.updateShippingStatus(ShippingStatus.IN_LOCKER);<br>            return locker;<br>        }<br>        throw new NoLockerAvailableException(<br>                &quot;No locker of size &quot; + size + &quot; is currently available&quot;);<br>    }<br>}</pre><ul><li>findAvailableLocker(LockerSize size): Locates and returns an available locker of the specified size.</li><li>placePackage(ShippingPackage pkg, Date date): A convenience method that finds an available locker and assigns the package in one step. If no locker is available, it returns null.</li></ul><p><strong>Implementation choice:</strong> While the same result could be achieved by separately calling findAvailableLocker() and then manually assigning a package, providing helper methods such as placePackage() is beneficial. It provides atomic transaction-like behavior by encapsulating the find-and-assign operations in a single method call, ensuring thread safety and preventing inconsistent states that could occur if these operations were performed separately.</p><h3>BasicShippingPackage</h3><p>The BasicShippingPackage class represents a standard package within the locker system, implementing the ShippingPackage interface. It encapsulates essential package details such as order ID, user account, dimensions (width, height, depth), and shipping status. These attributes allow the system to determine where the package can be stored.</p><p>Here is the implementation of this class.</p><pre>public class BasicShippingPackage implements ShippingPackage {<br>    // Unique identifier for the order<br>    private final String orderId;<br>    // User account associated with this package<br>    private final Account user;<br>    private final BigDecimal width;<br>    private final BigDecimal height;<br>    private final BigDecimal depth;<br>    // Current status of the package<br>    private ShippingStatus status;<br><br>    // Creates a new shipping package with specified dimensions<br>    public BasicShippingPackage(<br>            String orderId, Account user, BigDecimal width, BigDecimal height, BigDecimal depth) {<br>        this.orderId = orderId;<br>        this.user = user;<br>        this.width = width;<br>        this.height = height;<br>        this.depth = depth;<br>        this.status = ShippingStatus.CREATED;<br>    }<br><br>    // Returns the current package status<br>    @Override<br>    public ShippingStatus getStatus() {<br>        return status;<br>    }<br><br>    // Updates the package status<br>    @Override<br>    public void updateShippingStatus(ShippingStatus status) {<br>        this.status = status;<br>    }<br><br>    // Determines the smallest locker size that can fit this package<br>    @Override<br>    public LockerSize getLockerSize() {<br>        for (LockerSize size : LockerSize.values()) {<br>            if (size.getWidth().compareTo(width) &gt;= 0<br>                    &amp;&amp; size.getHeight().compareTo(height) &gt;= 0<br>                    &amp;&amp; size.getDepth().compareTo(depth) &gt;= 0) {<br>                return size;<br>            }<br>        }<br>        throw new PackageIncompatibleException(&quot;No locker size available for the package&quot;);<br>    } // getter methods are omitted for brevity<br>}</pre><p>A key functionality of this class is determining the appropriate locker size for a package. The getLockerSize() method evaluates all available locker sizes (“Small,” “Medium,” “Large”) and returns the smallest suitable locker based on the package’s dimensions.</p><p>Additionally, the updateShippingStatus() method allows the locker system to communicate status updates (e.g., when a package is stored or retrieved) to the user.</p><h3>Account and AccountLockerPolicy</h3><p>The Account class represents a user’s account within the locker system. It stores basic account details, including the accountId, ownerName, the current usage charges, and the user’s lockerPolicy (an instance of AccountLockerPolicy), which defines the user-specific rules for locker usage.</p><p>The account usageCharges is used to track locker usage fees, and funds can be added dynamically using the addUsageCharge(BigDecimal amount) method.</p><pre>public class Account {<br>    private final String accountId;<br>    private final String ownerName;<br>    // Policy defining locker usage rules for this account<br>    private final AccountLockerPolicy lockerPolicy;<br>    // Total charges accumulated for locker usage<br>    private BigDecimal usageCharges = new BigDecimal(&quot;0.00&quot;);<br><br>    // Creates a new account with specified details and policy<br>    public Account(String accountId, String ownerName, AccountLockerPolicy lockerPolicy) {<br>        this.accountId = accountId;<br>        this.ownerName = ownerName;<br>        this.lockerPolicy = lockerPolicy;<br>    }<br><br>    // Adds a charge to the account&#39;s total usage charges<br>    public void addUsageCharge(BigDecimal amount) {<br>        usageCharges = usageCharges.add(amount);<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><p>The AccountLockerPolicy class encapsulates the rules for locker usage. It specifies:</p><ul><li>freePeriodDays: The number of days a user can store a package without incurring charges.</li><li>maximumPeriodDays: The maximum number of days a package can remain in a locker before further action is required (e.g., additional charges or forced retrieval).</li></ul><p>This policy allows customized locker usage limits, ensuring that different users can have varying storage privileges based on their account settings.</p><p>Check out the implementation of this class.</p><pre>public class AccountLockerPolicy {<br>    // Number of days of free storage<br>    final int freePeriodDays;<br>    // Maximum number of days a package can be stored<br>    final int maximumPeriodDays;<br><br>    // Creates a new locker policy with specified free and maximum periods<br>    public AccountLockerPolicy(int freePeriodDays, int maximumPeriodDays) {<br>        this.freePeriodDays = freePeriodDays;<br>        this.maximumPeriodDays = maximumPeriodDays;<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><h3>LockerManager</h3><p>The LockerManager class acts as a facade for the overall locker system. It serves as the central coordinator for the locker system, coordinating package assignments to lockers, package pick-ups, and locker releases. It interacts with Site, which oversees the locker inventory and handles the actual placement of packages in available lockers. The NotificationInterface is responsible for sending user notifications regarding package assignments and pickups.</p><p>Additionally, it maintains a Map&lt;String, Account&gt; to store customer accounts, which are used to track locker usage charges. To ensure secure and efficient package retrieval, it uses a Map&lt;String, Locker&gt; to map access codes to their corresponding lockers.</p><p>Below is the implementation of this class.</p><pre>public class LockerManager {<br>    // The site being managed<br>    private final Site site;<br>    // Service for sending notifications<br>    private final NotificationInterface notificationService;<br>    // Map of account IDs to account objects<br>    private final Map&lt;String, Account&gt; accounts;<br>    // Map of access codes to lockers<br>    private final Map&lt;String, Locker&gt; accessCodeMap = new HashMap&lt;&gt;();<br><br>    // Creates a new locker manager for a site<br>    public LockerManager(<br>            Site site, Map&lt;String, Account&gt; accounts, NotificationInterface notificationService) {<br>        this.site = site;<br>        this.accounts = accounts;<br>        this.notificationService = notificationService;<br>    }<br><br>    // Assigns a package to an available locker<br>    public Locker assignPackage(ShippingPackage pkg, Date date) {<br>        Locker locker = site.placePackage(pkg, date);<br>        if (locker != null) {<br>            accessCodeMap.put(locker.getAccessCode(), locker);<br>            notificationService.sendNotification(<br>                    &quot;Package assigned to locker&quot; + locker.getAccessCode(), pkg.getUser());<br>        }<br>        return locker;<br>    }<br><br>    // Processes package pickup using an access code<br>    public Locker pickUpPackage(String accessCode) {<br>        Locker locker = accessCodeMap.get(accessCode);<br>        if (locker != null &amp;&amp; locker.checkAccessCode(accessCode)) {<br>            try {<br>                BigDecimal charge = locker.calculateStorageCharges();<br>                ShippingPackage pkg = locker.getPackage();<br>                locker.releaseLocker();<br>                pkg.getUser().addUsageCharge(charge);<br>                pkg.updateShippingStatus(ShippingStatus.RETRIEVED);<br>                return locker;<br>            } catch (MaximumStoragePeriodExceededException e) {<br>                locker.releaseLocker();<br>                return locker;<br>            }<br>        }<br>        return null;<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><ul><li>assignPackage(ShippingPackage pkg, Date date): Assigns a package to an available locker via site.placePackage().</li><li>pickUpPackage(String accessCode): Retrieves the package if the provided access code matches the stored locker. It then:</li><li>Calculates charges based on locker usage duration.</li><li>Updates the user’s usage charges.</li><li>Releases the locker and marks the package as retrieved.</li></ul><p><strong>Best practices:</strong> In complex systems, a manager class like LockerManager is commonly used to centralize operations such as package assignment, retrieval, and notifications. However, to maintain a thin manager class, it is essential to delegate low-level operations, such as finding available lockers, storing packages, and enforcing locker policies, to specialized classes like Site and Locker.</p><h3>Deep Dive Topic</h3><p>Now that the basic design is complete, the interviewer might ask you some deep dive questions. Let’s check out some of these.</p><h3>Designing an extensible locker creation system</h3><p>Currently, lockers are created directly based on predefined sizes, which makes the system rigid and difficult to extend. For example, if we need to introduce a new locker size (e.g., XLARGE) or a specialized locker type (e.g., temperature-controlled lockers), we would have to modify multiple parts of the codebase where lockers are instantiated manually. This increases maintenance overhead and reduces flexibility.</p><p>To address this, we can use the Factory design pattern to centralize the creation logic for different types of lockers, making the system more modular and extensible.</p><p><em>Note</em>: To learn more about the Factory Pattern and its common use cases, refer to the <a href="https://bytebytego.com/courses/object-oriented-design-interview/design-a-shipping-locker#further-reading-factory-design-pattern"><strong>Further Reading</strong></a> section at the end of this chapter.</p><p>Here is the code for the LockerFactory class.</p><pre>class LockerFactory {<br>    // Creates a new locker of the specified size<br>    public static Locker createLocker(LockerSize size) {<br>        return switch (size) {<br>            case SMALL -&gt; new Locker(LockerSize.SMALL);<br>            case MEDIUM -&gt; new Locker(LockerSize.MEDIUM);<br>            case LARGE -&gt; new Locker(LockerSize.LARGE);<br>            case XLARGE -&gt; new Locker(LockerSize.XLARGE);<br>        };<br>    }<br>}</pre><p>Here’s how a LockerFactory class enhances the locker system:</p><ul><li><strong>Centralized object creation:</strong> The LockerFactory encapsulates the locker instantiation process, so any changes to locker creation (e.g., adding new sizes or types) are localized to this class.</li><li><strong>Extensibility:</strong> If we need to add new locker sizes or types in the future, the factory class can be easily updated, without modifying core business logic in other parts of the system.</li><li><strong>Improved readability and maintainability:</strong> Using a factory method to create lockers allows us to separate locker creation from locker usage logic, aligning with the Single Responsibility Principle.</li></ul><h3>Decoupling event handling with an event-driven approach</h3><p>Currently, the LockerManager directly invokes NotificationInterface.sendNotification() to notify customers when key events occur, such as package assignment or locker expiration. While this approach works, it tightly couples LockerManager with the notification system, meaning any changes to how notifications are sent require modifying LockerManager itself.</p><p>How can we improve the system design to make event handling more flexible and scalable?</p><p>To decouple event handling from LockerManager, we can use an event-driven approach where multiple components can subscribe to and respond to system events dynamically. Instead of directly calling sendNotification(), LockerManager will broadcast events to registered observers, ensuring a more modular and extensible system.</p><p>By implementing the Observer pattern, we can send notifications to customers or administrative staff when certain events occur, such as when a package is delivered or a locker exceeds its maximum usage period.</p><p><em>Note</em>: To learn more about the Observer Pattern and its common use cases, refer to the <a href="https://bytebytego.com/courses/object-oriented-design-interview/design-an-elevator-system"><strong>Elevator System</strong></a> chapter.</p><p>Instead of hardcoding the notification logic in the LockerManager, the locker system can maintain a list of observers. These observers can be various notification services (e.g., email, SMS) or other systems (e.g., analytics, metrics). When a key event occurs, the LockerManager can notify all registered observers, allowing the system to be easily extended without modifying the core logic.</p><p>Below is the implementation of the relevant interface and classes for the observer pattern.</p><ul><li><strong>Subject</strong>: LockerManagerChange, which maintains a list of observers and notifies them of state changes, such as package assignments.</li></ul><pre>class LockerManagerChange {<br>    // List of observers that will be notified of locker events<br>    private final List&lt;LockerEventObserver&gt; observers = new ArrayList&lt;&gt;();<br><br>    public void addObserver(LockerEventObserver observer) {<br>        observers.add(observer);<br>    }<br><br>    public void removeObserver(LockerEventObserver observer) {<br>        observers.remove(observer);<br>    }<br><br>    private void notifyObservers(String message, Account account) {<br>        for (LockerEventObserver observer : observers) {<br>            observer.update(message, account);<br>        }<br>    }<br><br>    // Assigns a package to an available locker and notifies observers.<br>    public void assignPackage(ShippingPackage pkg) {<br>        Locker locker = assignLockerToPackage(pkg);<br>        if (locker != null) {<br>            notifyObservers(&quot;Package assigned to locker&quot;, pkg.getUser());<br>        }<br>    }<br><br>    private Locker assignLockerToPackage(ShippingPackage pkg) {<br>        return null;<br>    }<br>}</pre><ul><li><strong>Observer</strong>: The LockerEventObserver interface, which defines the update() method that observers must implement to handle notifications.</li></ul><pre>// Interface for objects that need to be notified of locker events<br>public interface LockerEventObserver {<br>    // Updates the observer with a message and the affected account<br>    void update(String message, Account account);<br>}</pre><ul><li><strong>Concrete Observers</strong>: Classes like EmailNotification, which implement LockerEventObserver to send email alerts for specific events, enabling flexible and extensible event handling.</li></ul><pre>// Implementation of LockerEventObserver that sends email notifications<br>class EmailNotification implements LockerEventObserver {<br>    // Updates the observer with a message and sends it to the account owner<br>    public void update(String message, Account account) {<br>        // send email to account owner<br>    }<br>}</pre><h3>Wrap Up</h3><p>In this chapter, we designed a Shipping Locker System. We began by clarifying requirements through a structured Q&amp;A discussion, similar to an interviewer-candidate exchange. From there, we identified core objects and developed a class diagram to represent the system’s structure. We implemented the key components of the locker service, bringing the design to life.</p><p>In the deep dive section, we explored advanced topics, including how the Factory Pattern simplifies locker instantiation for future scalability and how the Observer Pattern decouples event handling, enabling flexible notifications.</p><p>Congratulations on getting this far! Now give yourself a pat on the back. Good job!</p><h3>Further Reading: Factory Design Pattern</h3><p>This section gives a quick overview of the design patterns used in this chapter. It’s helpful if you’re new to these patterns or need a refresher to better understand the design choices.</p><h3>Factory design pattern</h3><p>The Factory Method Pattern is a creational design pattern that provides an interface for creating instances of a class but allows subclasses to alter the type of objects that will be created.</p><p>In the shipping locker system, we use the Factory pattern through the LockerFactory class to centralize locker creation, enabling easy addition of new locker sizes (e.g., XLARGE) without modifying existing code. To illustrate the Factory pattern in another domain, the following example uses an e-commerce payment system.</p><p><strong>Problem</strong></p><p>Imagine building an e-commerce payment system that initially supports only credit card payments, with logic tied to a CreditCardPayment class. As demand grows for new payment options like digital wallets or cryptocurrency, adding them requires altering multiple parts of the tightly coupled codebase, increasing maintenance effort, and reducing flexibility.</p><p><strong>Solution</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*gaH19Z9jyGJgao8eepV3AQ.png" /></figure><p>The Factory Method pattern addresses this by replacing direct object creation (using the new operator) with a factory method that encapsulates instantiation. This allows new payment types to be added via subclasses without changing existing code, decoupling payment processing from the main system for better scalability and maintainability.</p><p><strong>When to use</strong></p><p>The factory design pattern is particularly useful in the following scenarios:</p><ul><li>Use the Factory Method when the specific types and dependencies of objects required by your code are unknown in advance.</li><li>Use the Factory Method to allow users of your library or framework to extend its internal components without modifying existing code.</li><li>Use the Factory Method to optimize resource utilization by reusing existing objects rather than creating new instances each time.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aacbac902f78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Object-Oriented Design Interview — Blackjack Game]]></title>
            <link>https://medium.com/@dibasbehera/object-oriented-design-interview-blackjack-game-75965410f6e9?source=rss-97dc6053d9e7------2</link>
            <guid isPermaLink="false">https://medium.com/p/75965410f6e9</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Dibas Behera]]></dc:creator>
            <pubDate>Fri, 15 May 2026 18:26:00 GMT</pubDate>
            <atom:updated>2026-05-15T18:26:00.695Z</atom:updated>
            <content:encoded><![CDATA[<h3>Design a Blackjack Game</h3><p>In this chapter, we will discuss the object-oriented design of the Blackjack game (also called “21”). Blackjack is a popular card game where the goal is to get a hand of cards that adds up to 21, or as close as possible, without going over. The game is a mix of strategy (deciding when to hit or stand) and luck (the cards you get), making it a captivating and iconic casino game.</p><p>Let’s gather the game’s requirements through a typical interview-style conversation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*ZPtn7V4Xh2Fm4MrNCQnK2g.png" /></figure><h3>Requirements Gathering</h3><p>Here’s an example of a typical prompt an interviewer might present:</p><p>“Picture yourself at a casino table, ready to play a round of Blackjack, also known as ‘21.’ At the start, you and other players place bets, and the dealer distributes two cards to each player, including themselves. You evaluate your hand, aiming to get as close to 21 as possible without going over, and decide whether to hit or stand. After all players make their moves, the dealer reveals their hand and hits until reaching at least 17, then stands. Behind the scenes, the game manages a deck of cards, tracks player actions, ensures fair dealing, and updates balances. Let’s design a Blackjack game system that handles all this.”</p><p><strong>Note:</strong> Blackjack has various rules (e.g., soft 17, double down, splitting). This document focuses on the technical design and object-oriented implementation of a simplified standard Blackjack game.</p><h3>Requirements clarification</h3><p>Here is an example of how a conversation between a candidate and an interviewer might unfold:</p><p><strong>Candidate:</strong> Should I design the game to support multiple players or just one player competing against the dealer?<br><strong>Interviewer:</strong> The game should support multiple players.</p><p><strong>Candidate:</strong> What happens after a player takes their turn?<br><strong>Interviewer:</strong> After each player takes their turn (“hit” or “stand”), the game should check if all players have either stood or busted (hand value exceeding 21). When this happens, the game should determine the winner by comparing each player’s hand value and settle the bets.</p><p><strong>Candidate:</strong> Should the dealer follow any specific rules for when to hit or stand?<br><strong>Interviewer:</strong> Yes, the dealer should continue to “hit” until their hand totals at least 17. Once they reach 17 or higher, they must “stand.”</p><p><strong>Candidate:</strong> How are bets handled in the game, and how are players paid?<br><strong>Interviewer:</strong> Players place their bets before the initial cards are dealt. Players who win receive a payout equal to their bet (e.g., a $10 bet wins $10, plus their original bet returned), while players who bust lose their bet.</p><h3>Requirements</h3><p>Based on the conversation, here are the key functional requirements we’ve identified.</p><ul><li>The game should support multiple players and a dealer.</li><li>Players should be dealt two cards at the beginning of the game.</li><li>Players should have the option to “hit” (request an additional card) or “stand” (keep their current hand).</li><li>Aces should be valued as 1 or 11, with the value chosen to optimize the player’s hand.</li><li>After each player’s turn, the game checks if all players have stood or busted. ​​Once all players have completed their turns, the dealer takes their turn, hitting until their hand totals at least 17, then standing. The game then determines the winner and settles the bets.</li><li>Players who win receive a payout equal to their bet (1:1), while players who bust lose their bet.</li></ul><p>Below are the non-functional requirements:</p><ul><li>The user interface must be intuitive, with clear prompts and visual feedback on game state to accommodate users with minimal Blackjack experience.</li></ul><p>With these requirements in hand, let’s map out the game’s flow using an activity diagram to visualize how it all comes together.</p><h3>Activity Diagram</h3><p>Understanding the flow of a game like Blackjack is crucial when designing its object-oriented structure, especially given the game’s mix of sequential steps and decision points. This is where an activity diagram comes into play. An activity diagram visually maps out the workflow of the game, capturing each action, decision, and transition in a clear, step-by-step manner.</p><p>In the context of Blackjack, this means outlining everything from dealing cards to determining winners, ensuring we account for all possible paths, such as a player busting or the dealer hitting until 17. Let’s look at the activity diagram for Blackjack, which captures this process in detail.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*QHfRw0vRfepgPpNk3P_INA.png" /></figure><p>Now that we’ve got a clear picture of the game’s flow, let’s break down the core objects we’ll need to bring this design to life.</p><h3>Identify Core Objects</h3><p>As we have done in earlier chapters, let’s enumerate the core objects.</p><ul><li><strong>BlackJackGame:</strong> The BlackJackGame class acts as the central entity of the game, managing the overall flow from start to finish. It is responsible for dealing cards, tracking player actions (“hit”, or “stand”), and determining the winner.</li><li><strong>Player:</strong> The Player interface represents each participant in the game, with concrete implementations as RealPlayer for humans tracking bets and balance, and DealerPlayer for the dealer, who does not place bets and must hit until reaching a hand value of 17 or higher, as per Blackjack rules.</li><li><strong>Hand:</strong> Each player is associated with a Hand class, which manages the cards they receive during the game. This class calculates all possible hand values based on the cards held. This is especially important when handling Aces, which can count to 1 or 11, depending on which value keeps the player’s hand value closer to 21 without exceeding it.</li><li><strong>Deck:</strong> The Deck class is responsible for managing the collection of cards used in the game. It shuffles the cards when a new round begins and provides a new card when a player requests a hit.</li><li><strong>Card:</strong> Each individual card is represented by the Card class, which is defined by its Rank and Suit enums. The Rank determines the card’s value in the game, while the Suit provides its identity, such as “Hearts” or “Spades”.</li></ul><h3>Design Class Diagram</h3><p>Now that we know the core objects and their roles, the next step is to create classes and methods to build the Blackjack game.</p><h3>Card</h3><p>The Card class is a straightforward, immutable building block that holds a rank and a suit. It acts as a data-only entity with no behavior. This ensures immutability to prevent accidental changes and maintain game consistency.</p><p><em>Note</em>: Immutability means that once a card is created, its rank and suit cannot be changed.</p><p>Below is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*8tE_Rsl2-5VcvJXhVxUw1w.png" /></figure><p>The Card class is kept simple, it uses getRankValues() to fetch values from Rank. It leaves the heavy lifting of value calculations to the Hand class, sticking to a clean separation of duties. Note that the return value is a list of integers rather than a single value because Ace has two possible values (1 or 11).</p><p><strong>Design choice:</strong> The Card class is designed as a standalone entity to represent individual cards, enabling reuse across multiple decks or game variants.</p><p>Since Card relies on Rank and Suit to define its value and identity, let’s explore those next.</p><h4>Rank and Suit enumerations</h4><p>When modeling Rank and Suit, enums are the ideal choice as they’re type-safe, readable, and easy to maintain.</p><ul><li>Rank captures card values: numbers 2 through 10 are worth face value, Jack, Queen, and King are each worth 10, and an Ace can be either 1 or 11, depending on what benefits the hand most.</li><li>Suit lists the standard four options: Hearts, Diamonds, Clubs, and Spades.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/263/1*D1K4wMTjE7Uqsptai-tMug.png" /></figure><p><strong>Design choice: Why enums over other approaches?</strong> Consider using strings instead. You’d have a lot of flexibility, but that comes at a cost: extra validation to avoid invalid inputs, messy conversions when calculating hand values, and potentially higher memory usage. Integers might seem like a better alternative because they allow simple numeric representation (e.g., 1 for Ace, 10 for Jack), but they’re error-prone, developers might accidentally assign invalid values like 0 or 15, and their lack of inherent meaning requires additional checks, reducing the clarity that enums provide with named constants.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/279/1*37rsjMmoJoCwzEMf9nstXw.png" /></figure><p>Now that we’ve got a deck to draw from, let’s define the players who’ll use it.</p><h3>Player</h3><p>The Player interface serves as the blueprint for all participants in Blackjack, laying the foundation for both human players and the dealer. For human players, the RealPlayer class tracks essential details like a player’s name, hand, current bet, and balance, while providing methods for placing bets, receiving payouts, and retrieving key information. DealerPlayer, on the other hand, represents the house (no betting or balance here), just hitting until reaching 17 or higher, per Blackjack rules.</p><p>Here is the representation of this interface with concrete classes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/315/1*lA9LUIbPoELqLIeS-sHDEg.png" /></figure><p><strong>Design choice:</strong> An interface-based design for Player is chosen to abstract common behaviors across human players and the dealer, promoting extensibility.</p><p>Now that our players are set, let’s look at how they’ll manage their cards with the Hand class.</p><h3>Hand</h3><p>The Hand class manages the cards for a player or dealer in Blackjack, keeping track of a list of cards and calculating all possible hand values. It smartly handles Aces as either 1 or 11 to keep the hand as close to 21 as possible without going over. To support this, the class offers methods to add cards, access the card list, retrieve the possible values (stored as a sorted set to avoid duplicates), clear the hand for a new round, and determine if the hand is bust using isBust().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/330/1*MDF8Y_MzcASSEN8WsV8DPg.png" /></figure><p><strong>Design choice:</strong> Keeping Hand separate from Player keeps the design clean and modular by splitting responsibilities: Hand focuses solely on managing the cards and their values, while Player handles other player-specific details like bets and balance.</p><p>With hands managing cards and totals, let’s see how the BlackjackGame class ties it all together.</p><h3>BlackJackGame</h3><p>The BlackJackGame class is the central entity in our Blackjack game, orchestrating the action from the initial card dealing to the end of each round. It oversees the game’s deck, the players, and the dealer, handling card distribution, tracking turns, and wrapping up rounds by deciding winners and settling bets. To keep things fair, it ensures the dealer keeps hitting until reaching 17, then holds, while players get to choose whether to hit or stand.</p><p>Here is the representation of this class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*pwwzWEsZfnyuowQMH54eqA.png" /></figure><p>Now that we’ve detailed our key classes, let’s put them together in a complete class diagram to see the full picture.</p><h3>Complete Class Diagram</h3><p>Below is the complete class diagram of the Blackjack game:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*MhB0-5hz3iA5FgmSM7EYxg.png" /></figure><p>Let’s turn this design into working code.</p><h3>Code — Blackjack</h3><p>In this section, we will implement the core functionality of the Blackjack game, emphasizing critical components such as deck management for card distribution, turn coordination for players and the dealer, bet resolution, and winner determination through hand value comparison.</p><h3>Card</h3><p>The Card class is a straightforward representation of a playing card, combining a Rank and a Suit. It’s designed to be immutable. Its attributes stay fixed once created. The getRankValues() method retrieves the card’s possible values from Rank (e.g., 1 or 11 for an Ace).</p><p>Below is the code implementation of this class:</p><pre>public class Card {<br>    public final Rank rank;<br>    public final Suit suit;<br><br>    public Card(Rank rank, Suit suit) {<br>        this.rank = rank;<br>        this.suit = suit;<br>    }<br><br>    public int[] getRankValues() {<br>        return rank.getRankValues();<br>    }<br>}</pre><p>Rank is an enum that defines card values: Aces can be 1 or 11, numbered cards retain their face value, and face cards (Jack, Queen, King) are all worth 10. It stores these values in an array and provides them through getRankValues().</p><pre>public enum Rank {<br>    ACE(new int[] `{1, 11}`),<br>    TWO(new int[] `{2}`),<br>    THREE(new int[] `{3}`),<br>    FOUR(new int[] `{4}`),<br>    FIVE(new int[] `{5}`),<br>    SIX(new int[] `{6}`),<br>    SEVEN(new int[] `{7}`),<br>    EIGHT(new int[] `{8}`),<br>    NINE(new int[] `{9}`),<br>    TEN(new int[] `{10}`),<br>    JACK(new int[] `{10}`),<br>    QUEEN(new int[] `{10}`),<br>    KING(new int[] `{10}`);<br><br>    private final int[] rankValues;<br><br>    Rank(int[] rankValues) {<br>        this.rankValues = rankValues;<br>    }<br><br>    // Returns the possible values for the rank<br>    public int[] getRankValues() {<br>        return this.rankValues;<br>    }<br>}</pre><p>For Rank, we define Ace with [1, 11] from the start, rather than defaulting to 11 and adjusting later if the hand busts. This allows the Hand class to calculate all possible totals upfront, which is useful for handling multiple Aces.</p><p>Suit is a simple enum representing the four standard suits, Hearts, Spades, Clubs, and Diamonds. It acts purely as a label, giving each card its suit without additional logic.</p><pre>public enum Suit {<br>    HEARTS,<br>    SPADES,<br>    CLUBS,<br>    DIAMONDS<br>}</pre><p>With cards ready, let’s bundle them into a Deck for gameplay.</p><h3>Deck</h3><p>The Deck class represents a full deck of playing cards and provides essential operations such as shuffling, drawing, and resetting. It maintains a structured collection of Card objects and ensures that cards are drawn in the correct sequence.</p><p>Here is the implementation of this class:</p><pre>public class Deck {<br>    int nextCardIndex = 0;<br>    List&lt;Card&gt; cards;<br><br>    // Constructor initializes the deck<br>    public Deck() {<br>        initializeDeck();<br>    }<br><br>    // Initializes the deck with all cards<br>    private void initializeDeck() {<br>        cards = new ArrayList&lt;&gt;();<br>        for (Suit suit : Suit.values()) {<br>            for (Rank rank : Rank.values()) {<br>                cards.add(new Card(rank, suit));<br>            }<br>        }<br>        nextCardIndex = 0; // Reset to start drawing from the first card<br>    }<br><br>    // Shuffles the deck using current time as seed<br>    public void shuffle() {<br>        Collections.shuffle(cards, new Random(System.currentTimeMillis()));<br>    }<br><br>    // Draws the next card from the deck<br>    public Card draw() {<br>        if (isEmpty() || nextCardIndex &gt;= cards.size()) {<br>            throw new IllegalStateException(&quot;No more cards in deck&quot;);<br>        }<br>        Card drawCard = cards.get(nextCardIndex);<br>        nextCardIndex++;<br>        return drawCard;<br>    }<br><br>    // Returns the number of remaining cards in the deck<br>    public int getRemainingCardCount() {<br>        return cards.size() - nextCardIndex;<br>    }<br><br>    // Checks if the deck is empty<br>    public boolean isEmpty() {<br>        return getRemainingCardCount() == 0;<br>    }<br><br>    // Resets the deck to start drawing from the beginning<br>    public void reset() {<br>        nextCardIndex = 0;<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><p>The deck is initialized by pairing each Suit with every Rank, creating a complete set of 52 cards. These cards are stored in a List&lt;Card&gt;, which provides an efficient way to manage them.</p><p><strong>Implementation choice:</strong> Instead of removing cards when drawn, the deck tracks the next available card using nextCardIndex. This avoids the costly list operation of removing elements from an ArrayList, which requires shifting all remaining elements (O(n) complexity. By simply incrementing an index, drawing a card becomes an O(1) operation, improving performance.</p><p>Now that we’ve got cards flowing from the deck, let’s see how they land in a player’s Hand.</p><h3>Hand</h3><p>The Hand class is responsible for managing a player’s cards and computing possible hand totals, particularly when dealing with Aces, which can be worth 1 or 11. To achieve this, it maintains a List&lt;Card&gt; for tracking the cards in the hand and a SortedSet&lt;Integer&gt; to store all possible hand values dynamically.</p><p>When a new card is added via addCard(), it updates both the list of cards and the possible hand values. Handling Aces correctly is crucial:</p><ul><li>If the card is an Ace, both 1 and 11 are introduced as potential hand values.</li><li>If it’s a non-Ace, its value is added to every existing total, generating all possible hand values.</li></ul><p>This approach precomputes all valid hand scores upfront, avoiding unnecessary recalculations during gameplay and ensuring that multiple Aces are handled efficiently.</p><pre>public class Hand {<br>    final List&lt;Card&gt; handCards = new ArrayList&lt;&gt;();<br><br>    // Sorted set of all possible hand values, accounting for Ace flexibility (1 or 11).<br>    final SortedSet&lt;Integer&gt; possibleValues = new TreeSet&lt;&gt;();<br><br>    public Hand() {}<br><br>    // Adds a card to the hand and updates the set of possible total values.<br>    // For Aces (1 or 11), computes all combinations with existing totals; for other cards, adds<br>    // their value to each total.<br>    public void addCard(Card card) {<br>        if (card == null) {<br>            throw new IllegalArgumentException(&quot;Cannot add null card to hand&quot;);<br>        }<br>        handCards.add(card);<br><br>        // card.getRankValues() returns [1, 11] for Aces or a single value (e.g., [10]) for others.<br>        if (possibleValues.isEmpty()) {<br>            // Initialize with the card&#39;s values<br>            for (int value : card.getRankValues()) {<br>                possibleValues.add(value);<br>            }<br>        } else {<br>            // Add all possible card values to each existing total<br>            SortedSet&lt;Integer&gt; newPossibleValue = new TreeSet&lt;&gt;();<br>            for (int value : possibleValues) {<br>                for (int cardValue : card.getRankValues()) {<br>                    newPossibleValue.add(value + cardValue);<br>                }<br>            }<br>            possibleValues.clear();<br>            possibleValues.addAll(newPossibleValue);<br>        }<br>    }<br><br>    // Returns an unmodifiable list of cards in the hand<br>    public List&lt;Card&gt; getCards() {<br>        return Collections.unmodifiableList(handCards);<br>    }<br><br>    // Returns an unmodifiable sorted set of possible hand values<br>    public SortedSet&lt;Integer&gt; getPossibleValues() {<br>        return Collections.unmodifiableSortedSet(possibleValues);<br>    }<br><br>    // Clears the hand and possible values<br>    public void clear() {<br>        handCards.clear();<br>        possibleValues.clear();<br>    }<br><br>    // Checks if the hand is bust (all possible values &gt; 21)<br>    public boolean isBust() {<br>        // check if all possible value of the player&#39;s hand is busted<br>        if (possibleValues.isEmpty()) {<br>            return false;<br>        } else {<br>            return possibleValues.first() &gt; 21;<br>        }<br>    }<br>}</pre><p>The isBust() method determines whether a player has exceeded 21. It evaluates the lowest value in possibleValue, and if all options are above 21, the hand is considered bust. This is a critical check that immediately signals when a player is out of the game.</p><p><strong>Implementation strategy:</strong> Handling Aces efficiently is the biggest challenge in Blackjack hand management. Instead of recalculating Ace values dynamically each time a hand is evaluated, the Hand class precomputes all possible totals upfront. This allows for faster, more efficient scoring and ensures that Blackjack’s unique Ace logic is handled seamlessly without requiring mid-game adjustments.</p><p><strong>Data structure choice:</strong> A SortedSet (implemented as TreeSet) is used for possibleValues to maintain sorted hand values, enabling O(log n) insertion and O(1) access to the lowest value for isBust(). Alternatively, a HashSet offers O(1) insertion but lacks sorting, requiring O(n) to find the minimum. Similarly, a List could store values but would need O(n) for sorting or searching, which is less efficient for frequent checks in Blackjack’s fast-paced gameplay.</p><p>With hands tracking cards and totals, let’s define the Players who’ll hold them.</p><h3>Player</h3><p>In Blackjack, players fall into two distinct categories: human players, who place bets and manage their funds, and the dealer, who follows fixed rules and does not participate in betting. The Player interface and its concrete implementations, RealPlayer and DealerPlayer, provide a structured way to model these roles.</p><p>Below is the implementation of this interface.</p><pre>public interface Player {<br>    void bet(int bet);<br><br>    void loseBet();<br><br>    void returnBet();<br><br>    void payout();<br><br>    boolean isBust();<br><br>    Hand getHand();<br><br>    int getBalance();<br><br>    String getName();<br><br>    int getBet();<br>}</pre><p>The RealPlayer class models a human player who engages in betting and managing their funds. It implements the Player interface, ensuring that bet handling is managed separately from the core game logic. When placing a bet, the bet() method ensures that the bet does not exceed the player’s available balance before deducting the amount.</p><p>To maintain a clear separation of concerns, RealPlayer does not handle card evaluation directly. Instead, it delegates all card-related operations to the Hand class. This ensures that the player’s bet handling and card logic remain separate, making the class more modular and maintainable.</p><pre>public class RealPlayer implements Player {<br>    private final String name;<br>    private final Hand hand;<br>    private final int bet;<br>    private final int balance;<br><br>    public RealPlayer(String name, int startBalance) {<br>        this.name = name;<br>        this.hand = new Hand();<br>        this.bet = 0;<br>        this.balance = startBalance;<br>    }<br><br>    // Places a bet for the player<br>    @Override<br>    public void bet(int bet) {<br>        if (bet &gt; balance) {<br>            throw new IllegalArgumentException(&quot;Bet is greater than balance&quot;);<br>        }<br>        this.bet = bet;<br>        this.balance -= bet;<br>    }<br><br>    // Handles the player losing a bet<br>    @Override<br>    public void loseBet() {<br>        this.bet = 0;<br>    }<br><br>    // Handles returning the player&#39;s bet<br>    @Override<br>    public void returnBet() {<br>        this.balance += bet;<br>        this.bet = 0;<br>    }<br><br>    // Handles the player winning a payout<br>    @Override<br>    public void payout() {<br>        this.balance += bet * 2; // Return bet plus equal amount<br>        this.bet = 0;<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><p>The DealerPlayer class represents the house and follows predefined rules that differ from those of human players. Since the dealer does not participate in betting, the bet-handling methods, bet(), and loseBet() are implemented as empty functions, as they are never invoked in gameplay. Similarly, getBalance() and getBet() always return 0, reflecting the fact that the dealer does not manage a balance or place bets.</p><pre>public class DealerPlayer implements Player {<br>    private final String name = &quot;Dealer&quot;;<br>    private final Hand hand;<br><br>    public DealerPlayer() {<br>        this.hand = new Hand();<br>    }<br><br>    // Bet-handling methods for Dealer (bet, loseBet, returnBet) are implemented as empty functions.<br><br>    @Override<br>    public void payout() {<br>        // Dealer does not get a payout, so this method only prints the winning hand<br>    }<br>    // getter methods are omitted for brevity<br>}</pre><p>With players and their hands set, let’s orchestrate the whole game in BlackJackGame.</p><h3>BlackjackGame</h3><p>The BlackJackGame class serves as the central orchestrator of the game, handling players, the dealer, the deck, turn management, and enforcing the game rules. It ensures that the game follows a structured flow, with each player’s actions and the final outcome being resolved according to the rules of Blackjack.</p><p>Below is the implementation of this class.</p><pre>public class BlackJackGame {<br>    private final Deck deck = new Deck();<br>    private final List&lt;Player&gt; players = new ArrayList&lt;&gt;();<br>    protected final Player dealer = new DealerPlayer();<br>    private Player currentPlayer = null;<br><br>    // Tracks the current status of each player&#39;s turn (e.g., HIT or STAND)<br>    Map&lt;Player, Action&gt; playerTurnStatusMap = new HashMap&lt;&gt;();<br>    GamePhase currentPhase = GamePhase.STARTED;<br><br>    public BlackJackGame(List&lt;Player&gt; players) {<br>        for (Player player : players) {<br>            if (player == null) throw new IllegalArgumentException();<br>            this.players.add(player);<br>            this.playerTurnStatusMap.put(player, null);<br>        }<br>        this.playerTurnStatusMap.put(dealer, null);<br>        deck.shuffle(); // Shuffle the deck when game starts<br>    }<br><br>    // Determines the next player who can take an action (i.e., has not stood or bust). If the<br>    // current player is the dealer, it triggers the dealer&#39;s turn.<br>    public Player getNextEligiblePlayer() {<br>        // If current player hasn&#39;t stood or bust, they can continue their turn<br>        if (currentPlayer != null<br>                &amp;&amp; !Action.STAND.equals(playerTurnStatusMap.get(currentPlayer))<br>                &amp;&amp; !currentPlayer.isBust()) {<br>            return currentPlayer;<br>        }<br><br>        // Find the first player who hasn&#39;t stood or bust<br>        if (currentPlayer == null) {<br>            for (Player player : players) {<br>                if (!Action.STAND.equals(playerTurnStatusMap.get(player)) &amp;&amp; !player.isBust()) {<br>                    currentPlayer = player;<br>                    return currentPlayer;<br>                }<br>            }<br>        }<br><br>        // else, find the next player after the current one who hasn&#39;t stood or bust<br>        int currentPlayerIndex = players.indexOf(currentPlayer);<br>        for (int i = currentPlayerIndex + 1; i &lt; players.size(); i++) {<br>            Player player = players.get(i);<br>            if (!Action.STAND.equals(playerTurnStatusMap.get(player)) &amp;&amp; !player.isBust()) {<br>                if (currentPlayer == dealer) {<br>                    if (!Action.STAND.equals(playerTurnStatusMap.get(dealer))) dealerTurn();<br>                    return currentPlayer;<br>                }<br>                currentPlayer = player;<br>                return currentPlayer;<br>            }<br>        }<br><br>        // If no players are left to act, return null<br>        return null;<br>    }<br><br>    protected void dealerTurn() {<br>        // Dealer hits if below 17<br>        while (dealer.getHand().getPossibleValues().last() &lt; 17) {<br>            Card newDraw = deck.draw();<br>            dealer.getHand().addCard(newDraw);<br>        }<br>        playerTurnStatusMap.put(dealer, Action.STAND);<br>        checkGameEndCondition();<br>    }<br><br>    public void startNewRound() {<br>        deck.reset();<br>        for (Player player : playerTurnStatusMap.keySet()) {<br>            player.getHand().clear(); // Clear player&#39;s hand<br>        }<br>        dealer.getHand().clear(); // Clear dealer&#39;s hand<br>        // Reset all turn statuses to null<br>        playerTurnStatusMap.replaceAll((p, v) -&gt; null);<br>        currentPlayer = null; // Reset current player<br>        currentPhase = GamePhase.STARTED;<br>    }<br><br>    public void dealInitialCards() {<br>        if (!GamePhase.BET_PLACED.equals(currentPhase)) {<br>            throw new IllegalStateException(&quot;All players must bet before dealing&quot;);<br>        }<br>        // Deal first card to each real player in order<br>        for (Player player : players) {<br>            player.getHand().addCard(deck.draw());<br>        }<br>        // Deal first card to dealer<br>        dealer.getHand().addCard(deck.draw());<br>        // Deal second card to each real player in order<br>        for (Player player : players) {<br>            player.getHand().addCard(deck.draw());<br>        }<br>        // Deal second card to dealer<br>        dealer.getHand().addCard(deck.draw());<br>        currentPhase = GamePhase.INITIAL_CARD_DRAWN;<br>    }<br><br>    public void bet(Player player, int bet) {<br>        if (!GamePhase.STARTED.equals(currentPhase)) {<br>            throw new IllegalStateException(&quot;Bets must be placed at the start of the round&quot;);<br>        }<br>        player.bet(bet);<br>        // Transition to BET_PLACED once all players have bet<br>        if (players.stream()<br>                .filter(p -&gt; !(p instanceof DealerPlayer))<br>                .allMatch(p -&gt; p.getBet() &gt; 0)) {<br>            currentPhase = GamePhase.BET_PLACED;<br>        }<br>    }<br><br>    public void hit(Player player) {<br>        if (Action.STAND.equals(playerTurnStatusMap.get(player))) {<br>            throw new IllegalStateException(&quot;Player has already stood&quot;);<br>        }<br>        if (player.isBust()) {<br>            throw new IllegalStateException(&quot;Player is already bust&quot;);<br>        }<br><br>        Card drawnCard = deck.draw();<br>        player.getHand().addCard(drawnCard);<br>        playerTurnStatusMap.put(player, Action.HIT);<br>    }<br><br>    public void stand(Player player) {<br>        if (Action.STAND.equals(playerTurnStatusMap.get(player))) {<br>            throw new IllegalStateException(&quot;Player has already stood&quot;);<br>        }<br>        if (player.isBust()) {<br>            throw new IllegalStateException(&quot;Player is already bust&quot;);<br>        }<br>        playerTurnStatusMap.put(player, Action.STAND);<br>    }<br><br>    // Checks if the game has ended (all players done), then resolves bets by comparing each<br>    // player&#39;s hand to the dealer&#39;s.<br>    private void checkGameEndCondition() {<br>        boolean allPlayersDone =<br>                players.stream()<br>                        .allMatch(<br>                                p -&gt; Action.STAND.equals(playerTurnStatusMap.get(p)) || p.isBust());<br>        if (!allPlayersDone) {<br>            return;<br>        }<br><br>        int dealerValue = dealer.getHand().getPossibleValues().last();<br>        boolean dealerBusts = dealer.isBust();<br><br>        for (Player player : players) {<br>            if (player.isBust()) {<br>                player.loseBet();<br>            } else {<br>                int playerValue = player.getHand().getPossibleValues().last();<br>                if (dealerBusts || playerValue &gt; dealerValue) {<br>                    player.payout();<br>                } else if (playerValue == dealerValue) {<br>                    player.returnBet();<br>                } else {<br>                    player.loseBet();<br>                }<br>            }<br>        }<br>        currentPhase = GamePhase.END;<br>    }<br><br>    // getter methods are omitted for brevity<br>}</pre><ul><li>Each round begins with startNewRound(), which resets the deck, ensuring that players start with a clean slate.</li><li>Once the round starts, players place their bets using the bet() method, then dealInitialCards() distributes two cards per player, including the dealer. This guarantees a fair and structured start before betting and player actions begin.</li><li>The game progresses through player turns using getNextEligiblePlayer(), which selects the next active player who has not yet stood or busted. Once all players have finished their turns, control shifts to the dealer via dealerTurn().</li></ul><p>The dealer follows strict, predefined rules:</p><ul><li>The dealer must hit until their total reaches at least 17.</li><li>Once at 17 or higher, the dealer must stand.</li></ul><p>Players can take two primary actions during their turn:</p><ul><li>hit(): Draws a card and updates the player’s hand. If the player exceeds 21, they are marked as bust, eliminating them from further action.</li><li>stand(): Locks in the player’s hand, preventing further draws.</li></ul><p>The checkGameEndCondition() method finalizes the round once all players have stood or busted. It compares each player’s hand individually against the dealer’s:</p><ul><li>If the dealer busts (hand exceeds 21), non-bust players win and receive a payout.</li><li>For non-bust players, if their hand value exceeds the dealer’s (but is ≤ 21), they win and are paid out. If it’s lower, they lose their bet. In the case of a tie, return the bet to the player.</li></ul><p>Now that we’ve built a working game, let’s explore how we can make it more flexible for future changes</p><h3>Deep Dive Topic</h3><p>At this point, you’ve nailed the core requirements of the Blackjack game. This section will dive deeper into a potential extension to make the design more adaptable.</p><h3>Decoupling player and dealer decision logic</h3><p>In the current design, BlackJackGame directly controls player actions, calling hit() or stand() based on predefined conditions in the game logic. Similarly, the dealer’s “hit until 17” rule is hardcoded into dealerTurn(). This approach tightly couples decision-making with the game class, leaving little room for custom moves. Any rule modification, such as allowing a cautious player to stand at 12 or adjusting the dealer’s hit threshold, requires modifying BlackJackGame, leading to complex maintenance and potential bugs.</p><p>To resolve this, we introduce a decision-making abstraction that shifts control to individual players. This keeps BlackJackGame focused on coordinating turns, while each player determines their moves independently.</p><p><strong>Step 1: Define a decision-making interface</strong></p><p>To give each player control over their moves, we create a PlayerDecisionLogic interface with one method, decideAction(Hand), that picks ‘Hit’ or ‘Stand’ based on the hand.</p><pre>public interface PlayerDecisionLogic {<br>    // Decides the next action for a player based on their hand<br>    Action decideAction(Hand hand);<br>}</pre><p><strong>Step 2: Tailor decisions for humans and dealers</strong></p><p>With the interface in place, we implement two concrete decision classes:</p><ul><li>RealPlayerDecisionLogic: This captures a human player’s approach, say, hitting if the hand’s below 16.</li><li>DealerDecisionLogic: This locks in the dealer’s rule, hit if under 17.</li></ul><p>Here’s the code:</p><pre>public class RealPlayerDecisionLogic implements PlayerDecisionLogic {<br>    @Override<br>    public Action decideAction(Hand hand) {<br>        return hand.getPossibleValues().last() &lt; 16 ? Action.HIT : Action.STAND;<br>    }<br>}<br><br>public class DealerDecisionLogic implements PlayerDecisionLogic {<br>    @Override<br>    public Action decideAction(Hand hand) {<br>        return hand.getPossibleValues().last() &lt; 17 ? Action.HIT : Action.STAND;<br>    }<br>}</pre><p><strong>Step 3: Integrate decisions into players</strong></p><p>We update the Player interface with a getDecisionLogic() method, letting each player define its decision style. RealPlayer defaults to RealPlayerDecisionLogic, while DealerPlayer uses DealerDecisionLogic:</p><pre>public interface Player {<br>    // Returns the decision logic for the player<br>    PlayerDecisionLogic getDecisionLogic(); // ... other methods …<br>}<br><br>public class RealPlayer implements Player {<br>    private final PlayerDecisionLogic decisionLogic;<br><br>    public RealPlayer(String name, int startBalance) {<br>        this.name = name;<br>        this.hand = new Hand();<br>        this.bet = 0;<br>        this.balance = startBalance;<br>        this.decisionLogic = new RealPlayerDecisionLogic();<br>    }<br><br>    // Returns the decision logic for the player<br>    @Override<br>    public PlayerDecisionLogic getDecisionLogic() {<br>        return decisionLogic;<br>    }<br>    // ... other methods ...<br>}<br><br>public class DealerPlayer implements Player {<br>    private final PlayerDecisionLogic decisionLogic;<br><br>    public DealerPlayer() {<br>        this.hand = new Hand();<br>        this.decisionLogic = new DealerDecisionLogic();<br>    }<br><br>    // Returns the decision logic for the dealer<br>    @Override<br>    public PlayerDecisionLogic getDecisionLogic() {<br>        return decisionLogic;<br>    }<br>    // ... other methods ...<br>}</pre><p><strong>Step 4: Adjust the game flow</strong></p><p>In this step, we refactor the BlackJackGame code to use decision logic for both players and the dealer, streamlining the turn sequence. We introduce the performPlayerAction() method, which queries each player’s decision logic (RealPlayerDecisionLogic or DealerDecisionLogic) to decide whether to hit or stand. This replaces the hardcoded dealerTurn() method, integrating the dealer into the same flow as the players.</p><p>We also add the playNextTurn() method to coordinate turns, relying on getNextEligbilePlayer() to determine who acts next.</p><p>Here’s the updated code:</p><pre>public class BlackJackGame {<br>    // ... fields unchanged ...<br><br>    // Find the next player who can take an action<br>    public Player getNextEligiblePlayer() {<br>        // No current player: find first eligible player from the start<br>        if (currentPlayer == null) {<br>            for (Player player : players) {<br>                if (!Action.STAND.equals(playerTurnStatusMap.get(player)) &amp;&amp; !player.isBust()) {<br>                    currentPlayer = player;<br>                    return currentPlayer;<br>                }<br>            }<br>            // Instead of calling dealerTurn(), check if the dealer can act<br>            if (!Action.STAND.equals(playerTurnStatusMap.get(dealer))) {<br>                currentPlayer = dealer;<br>                return dealer;<br>            }<br>        } else {<br>            int currentIndex = players.indexOf(currentPlayer);<br>            for (int i = currentIndex + 1; i &lt; players.size(); i++) {<br>                Player player = players.get(i);<br>                if (!Action.STAND.equals(playerTurnStatusMap.get(player)) &amp;&amp; !player.isBust()) {<br>                    currentPlayer = player;<br>                    return currentPlayer;<br>                }<br>            }<br>            // If all players are done, check if the dealer can act<br>            if (currentPlayer != dealer &amp;&amp; !Action.STAND.equals(playerTurnStatusMap.get(dealer))) {<br>                currentPlayer = dealer;<br>                return dealer;<br>            }<br>        }<br>        return null; // All turns are complete, including the dealer&#39;s<br>    }<br><br>    // Executes the next turn by acting for the next player or dealer.<br>    public void playNextTurn() {<br>        Player nextPlayer = getNextEligiblePlayer();<br>        if (nextPlayer != null) {<br>            performPlayerAction(nextPlayer);<br>        }<br>    }<br><br>    // Performs the action decided by the player&#39;s decision logic (hit or stand).<br>    public void performPlayerAction(Player player) {<br>        Action action = player.getDecisionLogic().decideAction(player.getHand());<br>        if (action == Action.HIT) {<br>            hit(player);<br>        } else if (action == Action.STAND) {<br>            stand(player);<br>        }<br>    }<br>    // ... other methods unchanged, dealerTurn() removed ...<br>}</pre><p>The getNextEligiblePlayer() method now handles the full turn sequence: it returns the next player who hasn’t stood or busted, then the dealer once all players are done, and finally null when the dealer has stood, ending the turns. While dealerTurn() is removed, its logic lives on in DealerDecisionLogic, ensuring the dealer hits until 17 or higher.</p><p>This design shifts decision-making to the players, letting BlackJackGame coordinate turns while each player’s logic lives separately. If you’ve heard of the Strategy Pattern, you might notice this follows it, defining a set of decision rules that can swap in and out. Here:</p><ul><li>PlayerDecisionLogic sets the decision contract.</li><li>RealPlayerDecisionLogic and DealerDecisionLogic are the specific behaviors.</li><li>BlackJackGame uses them without needing to handle decisions internally.</li></ul><p><em>Note</em>: To learn more about the Strategy Pattern and its common use cases, refer to the <a href="https://bytebytego.com/courses/object-oriented-design-interview/design-a-parking-lot"><strong>Parking Lot</strong></a> chapter.</p><h3>Wrap Up</h3><p>In this chapter, we have built a solid Blackjack game from the ground up, with the key takeaway being how we structured responsibilities across Card, Deck, Hand, Player, and BlackJackGame into a clear, well-organized design. Each piece does its job, for instance, Card holds the essentials, Deck shuffles and deals, Hand tracks totals, and BlackJackGame runs the show. This approach keeps the game logical, easy to follow, and scalable.</p><p>We also took things further by decoupling decision-making with PlayerDecisionLogic, making it easy to swap strategies for players and the dealer. If you’re familiar with the Strategy pattern, you’ll recognize how it is applied here, giving the game flexibility without rewrites.</p><p>Congratulations on getting this far! Now give yourself a pat on the back. Good job!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=75965410f6e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>