Interview Question


Country: United States




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

Got a python3 solution here. Took under 30 minutes to make

x = [3, 6, 2, 7, 9, 5, 4, 1]
z = 1
sa = []
j = 0
for i in range(len(x)):
    v = 0
    j = i
    while j < (i + z):
        if j != len(x):
            v += x[j]
            j += 1
        else:
            break
    sa.append(v)
    if j != len(x):
        z += 1
    else:
        break
print(sa)

- Kidus Michael February 06, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my solution in python3

x = [3, 6, 2, 7, 9, 5, 4, 1]
z = 1
sa = []
j = 0
for i in range(len(x)):
    v = 0
    j = i
    while j < (i + z):
        if j != len(x):
            v += x[j]
            j += 1
        else:
            break
    sa.append(v)
    if j != len(x):
        z += 1
    else:
        break
print(sa)

- Kidus Michael February 06, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is my code in python3

x = [3, 6, 2, 7, 9, 5, 4, 1]
z = 1
sa = []
j = 0
for i in range(len(x)):
    v = 0
    j = i
    while j < (i + z):
        if j != len(x):
            v += x[j]
            j += 1
        else:
            break
    sa.append(v)
    if j != len(x):
        z += 1
    else:
        break
print(sa)

- Kidüs Michael February 06, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = [3, 6, 2, 7, 9, 5, 4, 1]

z = 1

sa = []

j = 0

for i in range(len(x)):

v = 0

j = i

while j < (i + z):

if j != len(x):

v += x[j]

j += 1

else:

break

sa.append(v)

if j != len(x):

z += 1

else:

break

print(sa)

- kidusmichael08 February 06, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

test_list = [3, 6, 2, 7, 9, 5, 4, 1]

result_list = []
counter = 0
for i in range(len(test_list)):
if counter
result_list.append(sum(test_list[i:2*i+1]))
counter = counter + 1

print(result_list)

- Anonymous February 09, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

test_list = [3, 6, 2, 7, 9, 5, 4, 1]

result_list = []
counter = 0
for i in range(len(test_list)):
if counter
result_list.append(sum(test_list[i:2*i+1]))
counter = counter + 1

print(result_list)

- Mahesh February 09, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I would like to check if the value 19 should be printed.
For 3 it's pos 0 so 3 + 0 (i = 0)
For 6 it's pos 1 so 6 + value in next [1+1] = 8
For 2 it's pos 2 so 2 + values in next 2 = 18
For 7 it's pos 3 so 7 + the values in the next 3 values = 25
For 9 it's pos 4 and if we continue the pattern it's 9 + the next 4 values but there are only 3, since the example doesn't continue for 5,4,1, it's unclear if you just add to the end if you have less than i left to the end of the array

- Anonymous February 12, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
        // test array
        int[] array = {3, 6, 2, 7, 9, 5, 4, 1};
        // list for result
        List<Integer> result = new ArrayList<>();
        for (int i = 0;  i < array.length; i++) {
            int sum = 0;
            // sum element on i-th place + j (where j is 0, +1, +2 until i-th place.)
            for (int j = 0; j <= i; j++) {
                // stop when no numbers left
                if (j+i >= array.length) {
                    break;
                }
                sum += array[j+i];
            }
            result.add(sum);
            // condition to not overlap original array
            if(2 * result.size() > array.length) {
                break;
            }

        }
        System.out.println(Arrays.toString(result.toArray()));
    }

- Marek February 15, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = [3, 6, 2, 7, 9, 5, 4, 1]
print([ sum(x[i:i+i+1]) for i in range(len(x))])

- Anonymous February 16, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x = [3, 6, 2, 7, 9, 5, 4, 1]
print([ sum(x[i:i+i+1]) for i in range(len(x))])

- HSC February 16, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

int main(void) {

int a[8]={3,6,2,7,9,5,4,1};
int b[4];
int i,j;
for(i=0;i<=4;i++)
{
b[i]=0;
int count=1;
for(j=i;j>=i&&count<=i+1;j++)
{
b[i]=b[i]+a[j];
count++;
}
}

for(i=0;i<=4;i++)
printf("%d ",b[i]);
return 0;
}

- nagamalla reddy February 19, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

int[] nums = { 3, 6, 2, 7, 9, 5, 4, 1 };
boolean cont = true;
for (int i = 0, k = 0; i < nums.length; i++, k++) {

if (cont) {
int sum = nums[i];
for (int j = i; j < (k + i); j++) {
if ((j + 1) < nums.length) {
sum += nums[j + 1];
} else {
cont = false;
}
}

System.out.println(sum);
}
}

}

- Pallab February 20, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {

		int[] nums = { 3, 6, 2, 7, 9, 5, 4, 1 };
		boolean cont = true;
		for (int i = 0, k = 0; i < nums.length; i++, k++) {

			if (cont) {
				int sum = nums[i];
				for (int j = i; j < (k + i); j++) {
					if ((j + 1) < nums.length) {
						sum += nums[j + 1];
					} else {
						cont = false;
					}
				}

				System.out.println(sum);
			}
		}

	}

- Pallab February 20, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def dfs(s):
res=[s[0]]
i=1
while i+i<=len(s):
j=i
r=0
while j<len(s) and j<=2*i:
r+=s[j]
j+=1
res.append(r)
i+=1
return res
s=[3, 6, 2, 7, 9, 5, 4, 1]
x=dfs(s)
print(x)

- 0405035.ms@ugrad.cse.buet.ac.bd February 22, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def dfs(s):
    res=[s[0]]
    i=1
    while i+i<=len(s):
        j=i
        r=0
        while j<len(s) and j<=2*i:
            r+=s[j]
            j+=1
        res.append(r)
        i+=1
    return res
s=[3, 6, 2, 7, 9, 5, 4, 1]
x=dfs(s)
print(x)

- 0405035.ms@ugrad.cse.buet.ac.bd February 22, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

All the solutions above are O(N^2). This one is O(N):

def calc_sum_i(arr_in):
   arr_len = len(arr_in)
   arr_out = [0] * arr_len
   # sum(i) = arr_in[i] + sum(i+1) - arr_in[2*i+1] - arr_in[2*i+2]
   for i in range(arr_len-1, -1, -1):
       sum_i = arr_in[i]
       if i < arr_len-1:
           sum_i += arr_out[i+1]
       if 2*i+1 <= arr_len-1:
           sum_i -= arr[2*i+1]
           if 2*i+2 <= arr_len-1:
               print("2*i+2")
               sum_i -= arr[2*i+2]
       arr_out[i] = sum_i
   return arr_out

- Anonymous February 23, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An O(N) algorithm:

def calc_sum_i(arr_in):
   arr_len = len(arr_in)
   arr_out = [0] * arr_len
   # sum(i) = arr_in[i] + sum(i+1) - arr_in[2*i+1] - arr_in[2*i+2]
   for i in range(arr_len-1, -1, -1):
       sum_i = arr_in[i]
       if i < arr_len-1:
           sum_i += arr_out[i+1]
       if 2*i+1 <= arr_len-1:
           sum_i -= arr[2*i+1]
           if 2*i+2 <= arr_len-1:
               print("2*i+2")
               sum_i -= arr[2*i+2]
       arr_out[i] = sum_i
   return arr_out

- shauli February 23, 2021 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

An O(N) solution:

def calc_sum_i(arr_in):
   arr_len = len(arr_in)
   arr_out = [0] * arr_len
   # sum(i) = arr_in[i] + sum(i+1) - arr_in[2*i+1] - arr_in[2*i+2]
   for i in range(arr_len-1, -1, -1):
       sum_i = arr_in[i]
       if i < arr_len-1:
           sum_i += arr_out[i+1]
       if 2*i+1 <= arr_len-1:
           sum_i -= arr[2*i+1]
           if 2*i+2 <= arr_len-1:
               sum_i -= arr[2*i+2]
       arr_out[i] = sum_i
   return arr_out

- shauligal February 23, 2021 | 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