🔥 Hot DP problem inside

ALSO: Design an Autocomplete System

Welcome back, Interview Masters!

This week’s challenge is a great way to understand Dynamic Programming better.

Today, we’ll cover:

  • House Robber problem

  • How to design an Autocomplete System?

Read time: under 5 minutes

CODING CHALLENGE

House Robber

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.

Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.

Example 1:

Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: nums = [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
Total amount you can rob = 2 + 9 + 1 = 12.

Solve the problem here before reading the solution.

SOLUTION

To solve this problem, we can use dynamic programming. We'll create an array max_amount where max_amount[i] represents the maximum amount of money we can rob from houses 0 to i.

For each house i, we have two options:

  1. Rob the current house i, in which case we cannot rob the previous house i-1. So the total amount of money would be nums[i] + max_amount[i-2] (if i-2 exists).

  2. Do not rob the current house i, in which case we can rob the previous house i-1. So the total amount of money would be max_amount[i-1].

We'll choose the option that gives us the maximum amount of money.

The time complexity of this solution is O(n) because we iterate through the nums array once.

PRESENTED BY ACI LEARNING

Tech Professionals are in High Demand 

Take your IT career to the next level and get ready for your next certification or brush up on your skills. Our video on-demand training library includes over 7,250 hours of courses plus hands-on skill labs and pre-certification practice tests.  

SYSTEM DESIGN EXPLAINED

How to design an Autocomplete System?

The main requirement is to provide the top 10 suggestions that match the user's partial query string, sorted by frequency and recency. The system needs to provide real-time suggestions within milliseconds, and scale to handle a large volume of traffic.

The system has two main API endpoints: get_suggestions to retrieve suggestion for a given prefix, and add_to_db to update the database with new trending queries.

To store and quickly retrieve query suggestions, we'll use a Trie data structure. A Trie is a tree where each node represents a character, allowing fast retrieval of words by traversing based on prefixes. We'll store the top 10 suggestions and their frequencies at each node.

To keep the Trie up-to-date, we'll log a sample of new search queries and run an offline MapReduce job periodically to update their frequencies in the Trie. We can use techniques like Exponential Moving Average to adjust for recent popularity changes.

The Trie will be partitioned across multiple servers by the first letter of the query. We'll use replicas and load balancers to distribute read traffic. This would also provide fault tolerance. Caching frequently searched terms can further improve read performance. You can dive into more details here.

NEWS

This week in the tech world

Source: Analytics India Mag

Nvidia Tops Microsoft as Most Valuable Firm: Thanks to the AI boom, Nvidia has surpassed Microsoft to become the world's most valuable public company at $3.34 trillion market cap. Nvidia's data center business revenue soared 427% year-over-year.

Bitcoin Dips Under $65K: Bitcoin fell back under $65,000 amid economic concerns and lower summer liquidity. Analysts cite uncertainty around the U.S. election as a factor impacting crypto markets in coming months.

Microsoft Delays AI Recall Tool: Microsoft will no longer ship its controversial Recall AI tool with the Copilot+ PC launch due to security concerns around the feature that tracks user activity, making it a preview for Windows Insiders first.

Zilch Raises $125M for BNPL Growth: British buy now, pay later firm Zilch raised $125 million from Deutsche Bank to triple sales and accelerate its path to an IPO in the next 12-24 months. The funding allows Zilch to draw down up to $315 million in credit.

Malaysia, a Data Center Hub: Malaysia is emerging as a major data center market in Asia, attracting billions in investments from tech giants like Google, Nvidia and Microsoft amid surging demand for cloud and AI services.

Amazon's $11B German Expansion: Amazon invests $11 billion to expand cloud services and logistics in Germany, highlighting the country's attractiveness amid economic pressures. The investment will create over 4,000 jobs.

Tempus AI IPO Pops 15%: Healthcare diagnostics AI firm Tempus AI rose as much as 15% in its Nasdaq debut after pricing at $37 per share and raising $410 million at a $6 billion valuation under the ticker "TEM."

BONUS

Just for laughs 😏

REFER FOR THE WIN

👋 Hey! We will host another Career Chat in July - date to come! Refer 10+ people to join a 30-minute Q&A session with Sahil. Looking forward to seeing many of you!

📌 Share your referral link on LinkedIn or with your friends to unlock the treasures quicker!
📌 Check your referrals status here.

YOUR FEEDBACK

What did you think of this week's email?

Your feedback helps us create better emails for you!

Login or Subscribe to participate in polls.

Until next time, take care! 🚀

Cheers,