Microsoft Interview Question

Hi folks,

I recently came across an interview question, and am stuck solving it. Can someone help me with this.

The question goes like this -
There are two people (p1 and p2). They are playing a game, where an array with some values is given.
p1 makes the first turn, and p2 makes the 2nd turn, and this goes on.
p1 play optimally, i.e. it always plays to select those value from the array which makes his sum maximum. p1 can pick any of the available value from the array.
p2 does not play optimally. p2 simply picks the first available value (from the left).

In the end we have to tell the score obtained by p1, and p2.

Eg: ; Let say array is- [8,3,9,4,15,7]
So, for this case, ans will be: p1 score = 32, and p2 score = 14.

Explanation: p1 picks 1st value, p2 picks 2nd
then p1 picks 3rd, p2 picks 4th,
then p1 picks 5th, p2 picks 6th

1 Like

Sorry, but this solution is completely wrong.

1 Like

ardamaxishere bro, can you provide a proper link to this question.

The question was asked in a recent OA. The test has already ended, and the question was not archived. Thus I can’t post the links.

import java.util.Scanner;
class Codechef{
public static void main(String arg[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt(),p1=0,p2=0;
int a[]=new int[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
if(i%2==0) {
p1+=a[i];
}
else {
p2+=a[i];
}
}
sc.close();
System.out.println(p1+" "+p2);
}
}

How About This?

You should have read the previous comments or at least checked the correctness for some other test cases.

Codeforces - The approach mentioned here seems correct to me.

1 Like

Can you please share other question as well?