Interview Question


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).

- Rayden January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- gladiator January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Rayden January 18, 2012 | Flag
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.

- Vineet Setia January 18, 2012 | Flag Reply
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;

- Raghu February 08, 2012 | Flag Reply
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).

- Sanjay Kumar January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Sanjay Kumar January 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous January 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

This comment has been deleted.

- Administrator January 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- gladiator January 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- eugene.yarovoi January 19, 2012 | Flag


Add a Comment
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.

Learn More

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.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More