## Interview Question

• 0

Country: India
Interview Type: In-Person

Comment hidden because of low score. Click to expand.
1
of 1 vote

This question has been answered a few days ago. After sorting you can find the maximum profit in O(n).

Comment hidden because of low score. Click to expand.
0

first : sorting will take O(nlogn) ...and secondly after sorting think about the worst case which will be O(N^2).

Comment hidden because of low score. Click to expand.
0

OK keep insisting on not checking the solution from a couple days ago.

Comment hidden because of low score. Click to expand.
0
of 0 vote

This can be done with O(n) complexity and for sorting we will take count sort...so overall complexity will be O(n)...and this is a modified version of some problem..I will upload it as soon as I recall it.

Comment hidden because of low score. Click to expand.
0
of 0 vote

I think we can do it in O(N) time.

int min_sofar = -1;
int max_profit = -1;
for (i = 0; i < n; i++) {
min_sofar = min(min_sofar, v[i]);
max_profit = max(max_profit, v[i]-min_sofar);
// If max_profit changes we can note down v[i],t[i] to separate variables to print later
}

return max_profit;

Comment hidden because of low score. Click to expand.
-1
of 3 vote

If I am understanding the question correctly
The main point is find out the time t1 having average value as MAX(Sell Point) and t2 having average value as MIN(Buy Point).

Comment hidden because of low score. Click to expand.
0

who ever is not agreeing with my comment please let me know that where I am wrong. In the whole question Its no where written that the data is from one day. else it will be simple question finding the time of min and max value and publish it.

there is possibility that data is from few days and we have to find out at which time the market is trading at low (buy point) and at which time market is trading at high, (sell point) If I was interviewed with this question then I will have asked this question. but with my understanding of the question my answer is correct. else give the whole question.

Comment hidden because of low score. Click to expand.
0

you have to buy shares to sell it.
so if maximum is at time 1 and min is at time 2.
you can't buy at time t1 and sell at time t2

Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

Comment hidden because of low score. Click to expand.
0

i was asked to do it O(n)...if u wish to find the solution in O(N^2) sorting is not necessary. :)

Comment hidden because of low score. Click to expand.
0

Sorting will be at least O(NlogN), though, unless you know the range of values and can do something akin to a counting sort.

Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

### Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

### Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.