DeepFrontend
Practice ArenaBlogSign in
Go Pro
DeepFrontend

Interview-grade learning paths and hands-on practice for mid-to-senior engineers. Master internals, patterns, and system architecture with runnable code.

All systems operational

Core Courses

  • -JavaScript Internals
  • -React Reconciliation
  • -TypeScript rigor
  • -Next.js Caching
  • -Node.js Event Loop
  • -System Design
  • -Web Security
  • -CSS & Page Layouts

Explore

  • Learning Paths
  • Practice Arena
  • Pricing Options
  • HTML Sitemap

Resources

  • Privacy Policy
  • Terms of Service
  • Email Support

© 2026 DeepFrontend. All rights reserved.

Expert learning environments for web engineering teams.

← All challenges
core12 min

Best Time to Buy and Sell Stock

Best Time to Buy and Sell Stock

Given prices where prices[i] is the price on day i, return the maximum profit from buying on one day and selling on a later day. If no profit is possible, return 0.

One pass: track the minimum price seen so far, and the best profit achievable selling at each day.

maxProfit([7, 1, 5, 3, 6, 4]); // 5  (buy at 1, sell at 6)
maxProfit([7, 6, 4, 3, 1]);    // 0  (prices only fall)

Run your code to see results.

Stuck? This challenge exercises:

javascript.data-structures