Can you crack Apple interview?

ALSO: What happens when you type a URL in your browser

Welcome back, Interview Masters!

We hope you’re ready for the coding challenge of this week.

In today’s newsletter, we’ll cover:

  • Best time to buy and sell stock - asked by Apple!

  • What happens when you type google . com on your browser?

Read time: under 4 minutes

CODING CHALLENGE

Best time to buy and sell stock

You are given an array prices where prices[i] is the price of a given stock on the ith day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.
Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

Solve the problem here before reading the solution.

SOLUTION

To maximize profit, we need to find the maximum difference between two elements in the array such that the larger element appears after the smaller element. This essentially means finding the lowest valley followed by the highest peak in the array.

We can do this by iterating through the array and keeping track of the minimum price seen so far. Then, for each element, we calculate the profit if we were to sell on that day (current price minus minimum price seen so far). If this profit is greater than the current maximum profit, we update the maximum profit.

The time complexity of the given code is O(n), where n is the number of elements in the prices list.

PRESENTED BY THE RUNDOWN AI

AI won’t take your job, but a person using AI might. That’s why 500,000+ professionals read The Rundown – the free newsletter that keeps you updated on the latest AI news and teaches you how to apply it in just 5 minutes a day.

SYSTEM DESIGN EXPLAINED

What happens when you type google . com on your browser?

When you type a URL in your browser and press enter, first the browser translates the domain name (google . com) to an IP address using a directory called DNS (Domain Name System).

Then, the browser establishes a secure connection with the server using TCP (Transmission Control Protocol) and HTTPS (Hypertext Transfer Protocol Secure).

The browser sends a request to the server specifying what action to take. For google . com, the action might be to submit a search or view a webpage. The server processes the request and sends back a response containing the requested data.

This response often includes HTML, CSS, and JavaScript files. The browser interprets the response and displays the webpage. To dive deeper, read the full article here.

NEWS

This week in the tech world

Source: Inc.Africa

Crypto Fraudster Sentenced: Sam Bankman-Fried, founder of FTX crypto exchange, received a 25-year jail sentence and an $11 billion fine for cryptocurrency fraud. This serves as a cautionary tale for the crypto industry.

Amazon Bets Big on AI: Amazon made its largest venture capital investment ever by spending an additional $2.75 billion in AI startup Anthropic, the developer behind the AI model Claude.

Bitcoin Price Tumbles: Bitcoin tumbles $5,000 in 24 hours early this week as interest rates jump. The price drop is likely caused by rising interest rates and a large Bitcoin transfer. April may be a volatile month for Bitcoin.

AWS Layoffs: Amazon is laying off this week hundreds of employees in its cloud computing unit, Amazon Web Services (AWS). The layoffs are a result of slowing sales growth in AWS. Amazon had laid off 27,000+ in 2022 and 2023, marking the largest workforce reduction in Amazon's history.

Waymo Delivers Uber Eats: Waymo and Uber Eats are partnering to offer self-driving delivery in Phoenix, Arizona. Users can track their deliveries on the Uber Eats app and Waymo will use its autonomous electric cars to deliver the food.

Tesla Share Drop: Tesla's deliveries in Q1 2024 declined year-over-year for the first time since 2020. The company delivered 386,810 vehicles, which is an 8.5% drop from Q1 2023. Production shutdowns and challenges in China contributed to the decline. Tesla stock fell 29% in Q1 2024.

POLL

Results from last week’s question

This one’s for you

How do you stay updated with tech trends?

Login or Subscribe to participate in polls.

BONUS

Just for laughs 😏

Source: @code_memez on X

SHARE YOUR THOUGHTS ON THIS EDITION

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.

Reviews of the week

Until next time, take care!

Cheers,

👋 Hey! Share your referral link with friends to unlock Hidden Treasures:

📌 Your referral link: https://www.interviewmaster.io/subscribe?ref=PLACEHOLDER

Share your referral link on LinkedIn or with your friends to unlock the treasures quicker.

NEXT IN CODING CHALLENGE

Maximum Subarray

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Join us next week to find out the solution.