bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch
🌱Newbie
0 XP
Back to starter track
✏️Fill the BlankjavascriptEasySliding Window

Starter: update a running best

Keep the smaller value as the best-so-far. Goal: Compare the saved best value with the next value and keep the smaller one.

Starter step 7 of 20Move through arrays

Track one useful value while scanning prices.

One variable remembers the lowest price; the answer compares against it.

Problem Brief

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.

Puzzle Hints
  1. Read the blank inside this exact statement: "best = Math.___(best, next);".

  2. A running minimum uses Math.min to keep the smaller value. The correct token works because it preserves the goal: Compare the saved best value with the next value and keep the smaller one.

  3. Check the variable that is read immediately after the blank; that usually tells you what the blank must produce.

Asked at 38 companies
AdobeAkuna CapitalAlibaba
Best Time to Buy/Sell — Sliding Min
two pointers
buysell701152336445
buy=0
sell=1
1
6

Track minimum buy price and maximum profit

How to think: Sliding Windowguide

You need the longest/shortest subarray or substring that satisfies a condition.

1.Ask: "Am I looking for a contiguous subarray/substring?" → Sliding window
2.Start: expand the right pointer to grow the window
3.When the window violates the condition: shrink from the left
4.Track your answer (max/min window size) at each valid state
5.Key: the window only grows right or shrinks left — never backtracks

vs Brute force (check all subarrays): You're looking at contiguous sequences — window avoids recomputing from scratch

vs Two Pointers: You need a range/subarray (not just two specific elements)

longest substringshortest subarraycontiguouswindow of size kat most k distinct
How to think: Sliding Windowguide

You need the longest/shortest subarray or substring that satisfies a condition.

1.Ask: "Am I looking for a contiguous subarray/substring?" → Sliding window
2.Start: expand the right pointer to grow the window
3.When the window violates the condition: shrink from the left
4.Track your answer (max/min window size) at each valid state
5.Key: the window only grows right or shrinks left — never backtracks

vs Brute force (check all subarrays): You're looking at contiguous sequences — window avoids recomputing from scratch

vs Two Pointers: You need a range/subarray (not just two specific elements)

longest substringshortest subarraycontiguouswindow of size kat most k distinct

Drag tokens into the blanks

min
max
1// Goal: Compare the saved best value with the next value and keep the smaller one.
2function starterExample() {
3 let best = 9;
4 const next = 4;
5 best = Math.___(best, next);
6}