Posts

Stock Span Problem (Solution in Java)

The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate the span of stock’s price for all n days. 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(including), 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}. Explanation to the given example: On 0th day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens. On 1st day only the current day where we find that stock price is less than or equal to it so for 1 consecutive day(current day) this happens. On 2nd day only the current day where we find that stock price is less than or equal ...

Nim Game Problem (Game is Love) with Solution

Let's say two players (p1, p2) are playing a game, in which they have two piles of stones N1 and N2. They take a turns by picking any even positive number of piles and keep half of them on other piles and throw another half. Whoever can not make a move loses. p1 makes the first move. Consider that both players will play optimized.  (Level: Hard) INPUT: N1 = 3 and N2 = 6 OUTPUT: p1 wins just (just print--> p1) EXPLANATION:  3 6 --- This is the case 4 4 --- move made by p1 2 5 --- move made by p2 3 3 --- move made by p1 1 4 --- move made by p2 2 2 --- move made by p1 1 3 --- move made by p2 2 1 --- move made by p1 0 2 --- move made by p2 1 0 --- move made by p1 Now as you can see, there is nothing to move (for p2) so that p1 win 's the game. Here is the solution in python :  Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you h...

Four Sum Problem (4sum) Solution in Java, Python, C++

Given an array and K, your task is to find four elements in an array such that their sum is equal to K. You have to print all four elements in lexicographical order and make sure that no pair get repeated. (Level: Hard) For Example:  N = 9 and array = [3, 6, 8, 2, 3, 9, 5, 1, 7] and K = 18 so, one pair is 1+3+5+9 = 18 you have to find all pairs A SOLUTION IN C++: You can optimize it further! A SOLUTION IN PYTHON :  AN OPTIMIZED SOLUTION IN JAVA :  Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems on Array and String, please check out these articles " Top 10 Array Interview Questions " and " Top 10 String Interview Questions ". All the Best!

Find the K'th last Node in Linked List

Given a linked list and an Integer K, your task is to find the Kth last node of the linked list. Input : 1->2->3->4->Null and K = 2 Output : 3 1. Native approach To solve this problem first we have to find a length of the linked list and then subtract K from it so you'll get K'th node from the front of the list, and then traverse again up to K. but wait! is this approach is efficient of course not, it takes O(N+(N-K)). 2. Two pointer approach First, take two pointers fast and slow, and move the fast pointer forward up to the first K node. Then again move forward (fast pointer) with a slow pointer so, when the fast pointer reaches to null, slow pointer reaches to Kth node from last. Let's see this in action (Implementation in Java). Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment...

Swap it (Sorting Question)

You have given a sequence of some elements p1, p2, p3, ..... pn, which is the permutation of {1,2,3.....n}. Rules: You can perform at most one swapping. Your task is to determine whether sequence can be represented in a sorted format or not. Solution: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems on array and string, please check out these articles " Top 10 Array Interview Questions " and " Top 10 String Interview Questions ". All the Best!

Reverse Words in a Sentence Without using any Library

Given a sentence or a string, our task is to reverse an order of words and print them. For example: Input: "python is love" Output: "love is python" The idea is to solve this problem is using split() and join() methods. First, split the sentence into words than change the order of words, then join then as one sentence. Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!

Find the length of the longest substring without repeating character

Given a string, our task is to find the length of the longest substring without repeating any character. For example, the longest substring for "ABACDBFB" this string is "ACDBF" length of this string is 5. This problem can be solved with O(N) time complexity where N is the length of the string. Let's implement that: Thank you so much for reading this article, if you like this question please share this with your friends who love programming. And if you have questions or feedback please drop a comment. To solve more problems, please check out this article " Top 10 String Interview Questions ". All the Best!