Interview Question


Country: United States
Interview Type: Written Test




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

This can be done easily in O(n) following the method explained below:

1. take a variable say, Sum, initialize it as Sum = 0.
2. For index i=1 to N do :
S[i] = Sum;
Sum+=A[i];
3. Sum = 0;
4. For index i=N down to 1 do:
S[i]+=Sum;
Sum+=A[i];
5. Done.

- Manohar Singh June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice answer Manohar. Yes it will give the desired result in O(n) time

- DashDash June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

Algo
1.take 2 arrays X and Y

where X[i]={A[1]+A[2]+ _ _ _ _ +A[i]}
AND Y[i}={A[i]+A[i+1]+A[i+2]+_ _ _ _ +A[n]}

THE ABOVE can be calculated in O(n) time

Now s[i]=x[i-1]+y[i+1]
Boundry conditions needs to be handled for i=0 and i=n

- blackfever June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is correct. aka is an idiot.

- Anonymous June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@blackfever: On a close inspection this really works.I was too rigid to notice that.
1 2 3 4
x[1] = 1 , x[2] = 1+2
y[1] = 1+ 2+3+4 y[2] = 2+ 3+4 y[3] = 3+4
s[1] = y[2] s[2] = x[1] + y[3]

- aka June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the correct answer. After a day of thinking, I came up with the same algorithm. It'll take two additional arrays and 3n time = O(n).
Good job, Blackfever!

- Denjar June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Denjar: Assuming you already have two arrays A (input), and S (output) this can be done without any additional array! Simply reuse S.

And it can be done in 2 passes, not 3.
Something like this:

sum = 0
s[0] = 0
// Arrays are 0 indexed.
for i in 0 to n-2
   sum += A[i]
   S[i+1] = sum 

sum = 0
for i = n-1 downto 1
    sum += A[i]
    S[i-1] = sum

- Anonymous June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The last line must be

S[i-1] += sum

- Anonymous June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is not a O(n) solution. Can some tell me how this is O(n) solution

- Praveen June 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@Praveen: You have no clue what O(n) means, do you?

- Anonymous June 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Traverse the array from index 1 to n, and calculate the sum. This step can be done in O(n). Let s be that sum.

Now for each index, do S[i] = s + (~A[i] +1)

~ operator inverts each bit of the value in A[i], converting it to it's 1's complement form. Adding 1 to ~A[i], evaluates the 2's complement of A[i]. We know (~A[i] +1) + A[i] = 0.
Hence (~A[i] +1) = -A[i]
Thus each S[i] = s - A[i]

- Anonymous June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anonymous: is not the question only asking us to use + operations only however I don't think it can ever become o(n) with that.

- aka June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aka: I guess I missed that. But I think someone might interpret not using minus operations as only using plus operator. But if, only plus operation is allowed, the solution is incorrect.

- Anonymous June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Denjar: are the numbes consecutive such as below?
a[] = {2, 3, 4,5}

- aka June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@aka: I think your solution is the one they would hav expected

- Pras June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Exercise1
    {

        int[] A = { 1, 2, 3 };
        int[] S = new int[3];


        public int calc(int sumOfNumbersBeforeI, int sumOfNumbersAfterI, int i)
        {
            int response = 0;
            if (i == A.Length - 1)
            {
                S[i] = sumOfNumbersBeforeI;
                sumOfNumbersAfterI = A[i];
                response = A[i];
            }
            else
            {
                sumOfNumbersAfterI = calc(sumOfNumbersBeforeI + A[i], sumOfNumbersAfterI, i+1);
                S[i] = sumOfNumbersBeforeI + sumOfNumbersAfterI;
                response = sumOfNumbersAfterI + A[i];
            }

            return response;

        }

        public void runme()
        {
            calc(0, 0, 0);
            foreach (int i in S)
                Console.Write(i+",");

            Console.ReadKey();

        }

    }

- andreas June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@blackfever: O(n) I don't think so

- aka June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@aka u forced me to write the complete code :)
int sumUptoi=a[0];
int x[]=new int[a.length];
x[0]=sumUptoi
i=1;
while(i<a.length)
{
x[i]=a[i]+=sumUptoi;
i++;
} //(o(n) asu can see)


int y[]=new int[a.length];
int sumfromi=a[a.length-1];
y[a.length-1]=a[a.length-1];

j=a.length-2;


while(j>=0)
{
y[j]=sumfromi+a[j]


j--;
}//(o(n) asu can see)



for(int i=0;i<a.length;i++)
{


if(i==0)
s[i]=y[i+1];
else if (i==a.length-1)
s[i]=x[i-1]

s[i]=x[i-1]+y[i+1]
}//this is also o(N)

- blackfever June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@blackfever: are you serious?What is the code you have written???

- aka June 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

for i = 1 to n
S += A[i];

for i = 1 to n
S[i] = S + (-A[i]); //Just Kidding
S[i] = S + (~A[i] + 1);

- coding.arya June 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ll
lkl

- bha January 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

jiji

- bha January 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int x;
cin>>sx;

- bha January 31, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

skfjklj
gwkjg
wgk;g
wgkekgw
agwh
wk

- bha January 31, 2018 | Flag Reply


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