## Oracle Interview Question for Senior Software Development Engineers

Country: India
Interview Type: In-Person

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

``````int A[] = {100, 180, 260, 310, 40,40, 535, 695};
//int A[] = {100, 90, 80, 70, 60, 30, 20,21};

int min = A[0]; // Lets assume first element is minimum
int maxProfit = 0; // 0 profit, if we buy & sell on same day.
int profit = 0;
int minIndex = 0; // Index of buy date
int maxIndex = 0; // Index of sell date

//Run the loop from next element
for (int i = 1; i < A.length; i++) {
//Keep track of minimum buy price & index
if (A[i] <= min) {
min = A[i];
minIndex = i;
}
//System.out.println("min: " + min);
profit = A[i] - min;
//If new profit is more than previous profit, keep it and update the max index
if (profit > maxProfit) {
maxProfit = profit;
maxIndex = i;
}
//System.out.println("max: " + A[maxIndex]);
}
if(maxProfit>0){
System.out.println("maxProfit is "+maxProfit);
System.out.println("buy @ "+ (minIndex+1) + " day");
System.out.println("sell @ "+ (maxIndex+1) + " day");
}
else{
System.out.println("No day where we can earn profit!");
}``````

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

``````int A[] = {100, 180, 260, 310, 40,40, 535, 695};
//int A[] = {100, 90, 80, 70, 60, 30, 20,21};

int min = A[0]; // Lets assume first element is minimum
int maxProfit = 0; // 0 profit, if we buy & sell on same day.
int profit = 0;
int minIndex = 0; // Index of buy date
int maxIndex = 0; // Index of sell date

//Run the loop from next element
for (int i = 1; i < A.length; i++) {
//Keep track of minimum buy price & index
if (A[i] <= min) {
min = A[i];
minIndex = i;
}
//System.out.println("min: " + min);
profit = A[i] - min;
//If new profit is more than previous profit, keep it and update the max index
if (profit > maxProfit) {
maxProfit = profit;
maxIndex = i;
}
//System.out.println("max: " + A[maxIndex]);
}
if(maxProfit>0){
System.out.println("maxProfit is "+maxProfit);
System.out.println("buy @ "+ (minIndex+1) + " day");
System.out.println("sell @ "+ (maxIndex+1) + " day");
}
else{
System.out.println("No day where we can earn profit!");
}``````

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