0% found this document useful (0 votes)
50 views3 pages

InfyTQ Practice Problem - Day 23

The document discusses three programming questions - finding stock buy/sell pairs that maximize profit given a stock price array, finding the length of the longest subarray with sum equal to 0 in a given array, and finding the longest common prefix among all strings in a given array.

Uploaded by

Shobhit Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views3 pages

InfyTQ Practice Problem - Day 23

The document discusses three programming questions - finding stock buy/sell pairs that maximize profit given a stock price array, finding the length of the longest subarray with sum equal to 0 in a given array, and finding the longest common prefix among all strings in a given array.

Uploaded by

Shobhit Tiwari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Day 23

Question 1: Stock buy and sell


The cost of stock on each day is given in an array A[] of size N. Find all the segments of days on which you buy
and sell the stock so that in between those days for which profit can be generated
.

Note: Since there can be multiple solutions, the driver code will print 1 if your answer is correct, otherwise, it
will return 0. In case there's no profit the driver code will print the string "No Profit" for a correct solution.

Example 1:

Input:
N=7
A[] = {100,180,260,310,40,535,695}
Output:
1
Explanation:
One possible solution is (0 3) (4 6) We can buy stock on day 0, and sell it on 3rd day, which will give us
maximum profit. Now, we buy stock on day 4 and sell it on day 6.

Example 2:

Input:
N=5
A[] = {4,2,2,2,4}
Output:
1
Explanation:
There are multiple possible solutions. one of them is (3 4) We can buy stock on day 3, and sell it on 4th day,
which will give us maximum profit.

Your Task:
The task is to complete the function stockBuySell() which takes an array A[] and N as input parameters and
finds the days of buying and selling stock. The function must return a 2D list of integers containing all the buy-
sell pairs i.e. first value of pair will represent the day on which you buy the stock and second value represent the
day on which you sell that stock. If there is No Profit, return an empty list.

Expected Time Complexity: O(N)


Expected Auxiliary Space: O(N)

Constraints:
2 ≤ N ≤ 106
0 ≤ A[i] ≤ 106
Question 2: Largest subarray with 0 sum
Given an array having both positive and negative integers. The task is to compute the length of the largest
subarray with sum 0.
Example 1:

Input:
N=8
A[] = {15,-2,2,-8,1,7,10,23}
Output: 5
Explanation: The largest subarray with sum 0 will be -2 2 -8 1 7.

Your Task:
You just have to complete the function maxLen() which takes two arguments an array A and n, where n is the
size of the array A and returns the length of the largest subarray with 0 sum.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= N <= 105
-1000 <= A[i] <= 1000, for each valid i
Question 3: Longest Common Prefix in an Array

Given a array of N strings, find the longest common prefix among all strings present in the array.

Example 1:

Input:
N=4
arr[] = {geeksforgeeks, geeks, geek,
geezer}
Output: gee
Explanation: "gee" is the longest common prefix in all the given strings.

Example 2:

Input:
N=2
arr[] = {hello, world}
Output: -1
Explanation: There's no common prefix in the given strings.

Your Task:
You don't need to read input or print anything. Your task is to complete the
function longestCommonPrefix() which takes the string array arr[] and its size N as inputs and returns the
longest common prefix common in all the strings in the array. If there's no prefix common in all the strings,
return "-1".

Expected Time Complexity: O(N*max(|arri|)).


Expected Auxiliary Space: O(max(|arri|)) for result.

Constraints:
1 ≤ N ≤ 103
1 ≤ |arri| ≤ 103

You might also like