In this video, we’ll solve the Stock Span Problem step by step using Stack in Java.
The Stock Span Problem is a very popular Data Structures and Algorithms (DSA) question frequently asked in coding interviews. You’ll learn the concept, logic, dry run, and Java implementation — all explained in a simple and beginner-friendly way.
📘 What is Stock Span Problem?
The Stock Span Problem can be defined as:
Given an array of daily stock prices, the span of a stock’s price on a given day is the maximum number of consecutive days (starting from that day and moving backward) for which the stock price was less than or equal to the current day’s price.
In simple terms:
It tells us how many days before today (including today) the price was not greater than today’s price.
📊 Example:
Let’s take an example to understand —
Input:
Price[] = {100, 80, 60, 70, 60, 75, 85}
Output:
Span[] = {1, 1, 1, 2, 1, 4, 6}
Explanation:
Day 1 → Price = 100 → No previous higher → Span = 1
Day 2 → Price = 80 → No previous ≤ 80 → Span = 1
Day 3 → Price = 60 → No previous ≤ 60 → Span = 1
Day 4 → Price = 70 → Previous 60 ≤ 70 → Span = 2
Day 5 → Price = 60 → No previous ≤ 60 → Span = 1
Day 6 → Price = 75 → 70, 60, 80 ≤ 75 → Span = 4
Day 7 → Price = 85 → 75, 70, 60, 80 ≤ 85 → Span = 6
🧠 What You’ll Learn in This Video:
✅ Definition and explanation of the Stock Span Problem
✅ How to solve it using Stack data structure
✅ Detailed dry run with explanation
✅ Full Java implementation
✅ Time and space complexity analysis
💡 Why Watch This Video?
This tutorial will help you master Stacks, Array traversal techniques, and algorithmic thinking. It’s perfect for students, beginners, and interview preparation.
🎯 BHARAT TECH SHOW – Learn Data Structures, Algorithms, and Core Programming Concepts in the simplest way possible!
💬 Subscribe for more coding tutorials, DSA topics, and Java series videos.
📢 Don’t forget to Like 👍, Share 🔁, and Subscribe 🔔 to BHARAT TECH SHOW for more amazing DSA content!
Информация по комментариям в разработке