• InstaByte
  • Posts
  • Tricky problem from Apple interview

Tricky problem from Apple interview

ALSO: Long Polling vs WebSockets vs Server-Sent Events

In partnership with

Welcome back!

Both Google and Apple asked an easy linked list problem. But is it really as easy as it looks? Let’s find out in this week’s coding challenge.

Today we will cover:

  • Middle of the Linked List problem

  • Long Polling vs WebSockets vs Server-Sent Events

Read time: under 4 minutes

JOIN OUR FREE COMMUNITY

We’re excited to announce that we’re launching an exclusive Discord community of programmers. We decided to create this community based on feedback we got from you a few days back.

As a member of this community, you will get

  • daily coding challenges

  • weekly contests to win prizes

  • weekly system design problems and much more

And did we mention that the community is free? 😊

THANK YOU for your constant support and feedback - Keep it coming!

Get early access by filling out this 30-second form. See you on Discord 😉

CODING CHALLENGE

Middle of the Linked List

Given the head of a singly linked list, return the middle node of the linked list.

If there are two middle nodes, return the second middle node.

Example:

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

Solve the problem here before reading the solution.

MEET 1440

Seeking impartial news? Meet 1440.

Every day, 3.5 million readers turn to 1440 for their factual news. We sift through 100+ sources to bring you a complete summary of politics, global events, business, and culture, all in a brief 5-minute email. Enjoy an impartial news experience.

SOLUTION

To solve this problem, we can use the two-pointer technique. We'll use two pointers: a slow pointer and a fast pointer. The slow pointer moves one step at a time, while the fast pointer moves two steps at a time.

When the fast pointer reaches the end of the list, the slow pointer will be at the middle node.

This is because the fast pointer travels twice as fast as the slow pointer. So, when the fast pointer reaches the end, the slow pointer will be halfway through the list.

The time complexity of this solution is O(n), where n is the number of nodes in the linked list.

SYSTEM DESIGN

Long Polling vs WebSockets vs Server-Sent Events

Let's talk about three different ways to get real-time updates in web applications, because choosing the right approach can make a huge difference in your app's performance.

Repeatedly asking the server for updates (Regular Polling isn't great because it wastes resources. Imagine refreshing the app every few seconds for a message that might never come. Your battery drains, and the server gets overwhelmed with unnecessary requests.

When your web app needs to receive live updates from a server, you have three main options: Long Polling, WebSockets, and Server-Sent Events (SSE).

Long Polling is smarter than Regular Polling. Instead of constantly checking, the client makes a request and the server holds onto it until there's actually new data to send. Once the server responds, the client immediately makes another request. While this is better than regular polling, it still has drawbacks because you need to create new connections frequently, which involves sending extra headers and using server resources.

WebSockets are more sophisticated. They create a persistent, two-way connection between the client and server. This means both sides can send data to each other at any time without establishing new connections. WebSockets are great for applications that need frequent, real-time updates like chat apps or live gaming. However, keeping these connections open can waste resources if you're not sending data frequently.

Server-Sent Events (SSE) offer a middle ground. Like WebSockets, they maintain a persistent connection, but they only allow the server to send data to the client (one-way communication). If the client needs to send data back, it uses regular HTTP requests. SSE also offers automatic reconnection if the connection drops. However, this can cause problems in large applications through something called the "Thundering Herd" problem - if the server goes down and comes back up, all clients trying to reconnect simultaneously might overwhelm it.

Here's a comparison of the three approaches:

Long Polling

WebSockets

Server-Sent Events

Connection

Temporary

Persistent

Persistent

Communication

One-way

Two-way

One-way

Auto-Reconnect

No

No

Yes

Header Overhead

High

Low

Low

Best For

Infrequent updates

Real-time, bi-directional communication

Server-to-client streaming

PRESENTED BY CODEIUM

Unlock Windsurf Editor, by Codeium.

Introducing the Windsurf Editor, the first agentic IDE. All the features you know and love from Codeium’s extensions plus new capabilities such as Cascade that act as collaborative AI agents, combining the best of copilot and agent systems. This flow state of working with AI creates a step-change in AI capability that results in truly magical moments.

NEWS

This Week in the Tech World

Source: Investor’s Business Daily

Amazon Launches Budget Store: Amazon debuts "Amazon Haul," a discount store with all items under $20, targeting Temu and Shein. Products ship directly from China with 2-week delivery and free shipping on orders over $25.

Bitcoin Hits Record High: Bitcoin surged above $91,000 for the first time, rising over 2% to $91,705. The cryptocurrency rallied following Donald Trump's election victory and October inflation data showing a 2.6% annual rate.

Musk's Trump Gains: Tesla's 39% stock surge since Trump's election victory adds $70 billion to Elon Musk's net worth. Musk, who spent $130 million supporting Trump, sees wealth rise to $320 billion.

Klarna Files for US IPO: Swedish buy now, pay later giant Klarna has filed for a US IPO. Recently valued at $15 billion, down from its $46 billion peak in 2021, Klarna aims to list in New York despite European exchanges' efforts.

Startup CEO “No humans needed for translation in 3 years“: Unbabel launches Widn AI translation service powered by its LLM Tower. CEO Vasco Pedro claims human translators won't be needed within three years due to AI advancements.

23andMe Cuts Jobs: Genetics company 23andMe announces 40% workforce reduction, cutting 200 jobs and ending therapeutics programs. Company reports declining revenue and seeks additional capital amid 75% stock drop.

Block Scales Back Crypto: Jack Dorsey's Block winds down its TBD crypto unit amid layoffs. Company maintains bitcoin holdings worth $630 million but shifts focus to business lending and Afterpay integration.

Meta Cuts EU Subscription Fees: Meta reduces Facebook and Instagram ad-free subscription prices by 40% in EU to comply with regulations. Monthly fees drop to €5.99 for desktop and €7.99 for mobile users.

TRENDING DEVELOPER TOOLS

Instantly add file uploads to your app with Pinata’s API

Pinata’s File API lets developers integrate file uploads and retrieval in just minutes. No complex setup, no configuration headaches—just fast and scalable file management.

HELP US

👋 Hi there! We are on a mission to provide as much value as possible for free. If you want this newsletter to remain free, please help us grow by referring your friends:

📌 Share your referral link on LinkedIn or directly with your friends.
📌 Check your referrals status here.

BONUS

Just for laughs 😏

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,