How to Solve Stock Span Problem Using Python16 Apr 2025 | 5 min read In this tutorial, we will solve the stock span problem using the Python code. Before proceeding to the solution, let's understand the problem statement of this problem. What is Stock Span Problem?The stock span problem is financial problem where we calculate the share price. In this problem, we have a series of n daily price quotes and we need to calculate the span of stock's price for all n days. Suppose the span Si of the stock's price on a given day I is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day. For example, if an array of 7 days prices is given as [100, 80, 60, 70, 60, 75, 85], then the span values for corresponding 7 days are [1, 1, 1, 2, 1, 4, 6]. Code Implementation - The following solution is simple but inefficient where we traverse the input price array. When we visit the every element, traverse elements on the left of it and increment the span value of it while elements on the left side are smaller. Example - Output: 1 1 2 4 5 1 The above method takes the 0(n2) to solve the problem. Let's try to reduce the time complexity by the 0(n) times. A Linear-Time Complexity MethodWe can make the above solution efficient using linear-time complexity method. In this method, S[i] where i can be calculated if we know the closed day preceding i, such that the price is greater than on that day than the price on the day i. If such condition occurs, let's call it h(i), otherwise, we define h(i) =-1. We will implement this logic using the stack data structure. Example - Output: 1 1 2 4 5 1 Explanation - In the above code, we defined the a getSpan() method that takes the lists as arguments. First we calculated the length of the price list and initialized the empty stack. We fixed the span value of the first element as 1. Then, we visited each element using for loop. In for loop iteration, we iterated while loop until pop elements from stack while stack is not empty and top of stack is smaller than price[i]. Then we checked, if the stack becomes empty, price[i] is greater than all elements on left of it, i.e. price[0], price[1]……price[i-1], otherwise the price[i] is greater than elements after top of stack. If the condition is true pushed element to the stack. We created a utility function to print elements of array. At the end, we initialized price and span list. Now called the getSpan() function along with the printList(). It printed the span values. Time ComplexityThe time complexity of this method will be 0(n) which is better than the previous one. We can see that every element of the array is added and removed from the stack at most once. Let's see another approach. Without Using StackOutput: 1 1 2 4 5 1 Explanation - In this approach, we created a getSpan() function that takes three arguments - List, length, and result. We fixed the first element as 1 and calculate the rest span values of the element. We defined the utility function to print the elements of the list. At the end, we called the getSpan() method and it returned the span values. ConclusionThis problem commonly asked in the technical interview and you can find this problem top coding practice sites like leetcode.com. In this tutorial, we have implemented the solution of span problem using the three approaches. First solution is quite simple but inefficient in terms of memory. Next TopicSelection Sort in Python |
Python provides many built-in methods to handle the precision of floating points. In this tutorial, we will discuss the most common types of methods to set precision in Python. Most of the methods are defined under the math module. Various Methods to handle Precision The following methods come...
2 min read
Binary language is the language of a computer. All the inner mechanisms of a computer happen concerning bits. Bitwise operators are the set of operators that allow the programmer to perform bitwise operations on integers. These operators allow the programmer to manipulate the lower-level data in...
3 min read
In this tutorial, we will discuss about the type hinting concepts which is helpful to improve code readability and enhance the code structure. We will discuss some tips of type hinting which will make the Python program more readable. As we know, Python is a dynamically-typed programming...
9 min read
This article will teach us how to use Python to track Amazon items we want to buy. We often purchase the item provided it goes under a particular limit to keep it affordable and expand our reserve funds. In this venture, we will precisely carry out...
4 min read
Bokeh is a library of Python which is used for data visualization by plotting graphs and charts. The output can be obtained on different platforms such as Notebook, HTML, JavaScript, and browser. The Figure Class is used for creating a new figure on the graph, and...
3 min read
An Introduction to Menu-Driven Program Menu-Driven Program is a program that gets input from a user by showing the options list, known as the menu, from which the user chooses their option. Systems processing the Menu-Driven programs are ordinary, starting from washing machines controlled by Microprocessors to...
12 min read
This tutorial will write a Python program to combine the two dictionaries with the common keys. Dictionary is a Python data structure that stores the data in the key-value pair. Each key should be immutable and unique in the dictionary. Let's understand the above problem statement. Problem...
2 min read
In pandas, a type of list called a series can incorporate number, string, twofold, and different sorts of information. Conversely, Pandas Series returns an item as a list, where n is the length of the series information and the record goes from 0 to n....
6 min read
? In Python, you can check if a dictionary is empty by using the built-in function len or comparing the dictionary to an empty dictionary {}. Here are two methods for checking if a dictionary is empty: Method 1: Using the len function d = {} if len(d) == 0: print("The...
2 min read
Python dictionary is a type of data structure that makes it simple to write highly effective code. Since we can hash its keys, a dictionary data structure is a type of hash table in several other languages. A data structure called a hash table uses associative...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India