Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,906 members, 7,802,945 topics. Date: Saturday, 20 April 2024 at 04:41 AM

Programming Interview Test - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Programming Interview Test (3596 Views)

Programming Interview Question: Invert A Binary Tree (2) (3) (4)

(1) (2) (3) (Reply) (Go Down)

Programming Interview Test by Nobody: 3:04pm On May 15, 2016
Edited: Below is an interview coding test:
In my opinion this should be a staff training exercise instead on an interview test.

This is a demo task.

A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.
A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

For example, consider the following array A consisting of N = 8 elements:

A[0] = -1
A[1] = 3
A[2] = -4
A[3] = 5
A[4] = 1
A[5] = -6
A[6] = 2
A[7] = 1
P = 1 is an equilibrium index of this array, because:
A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]

P = 3 is an equilibrium index of this array, because:
A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]

P = 7 is also an equilibrium index, because:
A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0

and there are no elements with indices greater than 7.

P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.

Write a function:

int solution(int A[]);

that, given a zero-indexed array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.

For example, given array A shown above, the function may return 1, 3 or 7, as explained above.

Assume that:

N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Complexity:

expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.

Source: https://codility.com/demo/take-sample-test/
Re: Programming Interview Test by tr3y(m): 10:28pm On May 15, 2016
It is kind of simple, the only problem is just the required O(N);
Re: Programming Interview Test by Nobody: 8:30am On May 16, 2016
^^^
The coding and O(N) is no big deal for a programmer with CS of SE.
Re: Programming Interview Test by Nobody: 8:50am On May 16, 2016
como se dice "CS del SE" en ingles?
Re: Programming Interview Test by Nobody: 9:27am On May 16, 2016
^^^
Na so e be o!
Re: Programming Interview Test by Nobody: 9:34am On May 16, 2016
How do you say "CS of SE" in English?
Re: Programming Interview Test by Nobody: 9:52am On May 16, 2016
^^^
Now moving from programming to Pro-Grammar grin
Re: Programming Interview Test by Nobody: 10:08am On May 16, 2016
HAHAHAHA. grin
Re: Programming Interview Test by Nobody: 10:09am On May 16, 2016
No comprende?
Re: Programming Interview Test by Nobody: 10:16am On May 16, 2016
dhtml18:
No comprende?
Did u dream of france last night
Re: Programming Interview Test by Nobody: 10:27am On May 16, 2016
France? uhm you are mistaken mi amigo, estoy spanish.
Re: Programming Interview Test by Nobody: 10:30am On May 16, 2016
I will break down the 1st part of the task just to make it simpler for those that might like to attempt it.

A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1]

Can be interpreted as: A[0] + A[1] + A[3-1] = A[3+1] + A[8−2] + A[8−1]

Where:
N = 3 which is the length of the array
and
P = 3
Re: Programming Interview Test by Nobody: 10:38am On May 16, 2016
dhtml18:
France? uhm you are mistaken mi amigo, estoy spanish.
OOPS! i thought it was french.
Re: Programming Interview Test by Nobody: 10:45am On May 16, 2016
Hehehehehehe, with some certain constructs, it is a bit hard to figure out whether it is french or spanish.

ne comprendre (french) === no comprende (spanish) === not understand (english)

I can only speak/write spanish to an extent, but my french is quite quite poor like an average Nigerian.

Lets come back to the quiz, it looks like Febup is trying to win o, crotonite show him that you are a true programmer jor!
Re: Programming Interview Test by Nobody: 10:52am On May 16, 2016
Currently busy at the moment @dhtml18. let him win, i will hustle 4 2nd or 3rd place.
Re: Programming Interview Test by Nobody: 11:06am On May 16, 2016
Chai, me too busy small - battling a native android app that streams live videos from a server - the thing don nearly finish me for here!
Re: Programming Interview Test by Nobody: 11:57am On May 16, 2016
^^^
Okay-dokay, after you finish the almighty android app you can come back and take the quiz.
Re: Programming Interview Test by Nobody: 12:10pm On May 16, 2016
dhtml18:
Chai, me too busy small - battling a native android app that streams live videos from a server - the thing don nearly finish me for here!
native as in c/c++
Re: Programming Interview Test by Nobody: 12:57pm On May 16, 2016
dhtml18:
Chai, me too busy small - battling a native android app that streams live videos from a server - the thing don nearly finish me for here!
what language boss I know you will soon build the app . hearing server I guess u are using html5 .
Re: Programming Interview Test by Nobody: 2:11pm On May 16, 2016
crotonite:
native as in c/c++
JAVA/Android Studio
Re: Programming Interview Test by larisoft: 2:16pm On May 16, 2016
Here...Just got this off the top of my head. I know it has flaws but till I get a better solution and I will:

public static int solution(int[] nums, int n){

if(sum(nums)==0) return 0;

int index = 0;
int i = 1;


while( i < nums.length){

int[] left = Arrays.copyOfRange(nums, 0, i);
int[] right = Arrays.copyOfRange(nums, i+1, nums.length);

//1, 2, 3, 5, 1, 2, 3, 0

if( sum(left)== sum(right)){
return i;
}
i++;
}

return -1;

}

private static int sum(int[] arr){

int a = 0;
for(int i: arr){
a+=i;
}
return a;
}
Re: Programming Interview Test by Nobody: 2:31pm On May 16, 2016
^^^O boy, see code o - this is the kind of codes that makes me start to dance alanta!
Re: Programming Interview Test by larisoft: 2:36pm On May 16, 2016
larisoft:

Here...Just got this off the top of my head. I know it has flaws but till I get a better solution and I will:

public static int solution(int[] nums, int n){

if(sum(nums)==0) return 0;

int index = 0;
int i = 1;


while( i < nums.length){

int[] left = Arrays.copyOfRange(nums, 0, i);
int[] right = Arrays.copyOfRange(nums, i+1, nums.length);

//1, 2, 3, 5, 1, 2, 3, 0

if( sum(left)== sum(right)){
return i;
}
i++;
}

return -1;

}

private static int sum(int[] arr){

int a = 0;
for(int i: arr){
a+=i;
}
return a;
}


This runs in O(n).

public static int solution2(int[] nums, int n){

int sum = sum(nums);

int i = 0;
int left_sum = 0;

while(i < nums.length){

int right_sum = sum - left_sum - nums[i];

if(right_sum == left_sum){
return i;
}

left_sum+= nums[i];
i++;

}

return -1;

}

private static int sum(int[] arr){

int a = 0;
for(int i: arr){
a+=i;
}
return a;
}
Re: Programming Interview Test by Nobody: 3:10pm On May 16, 2016
@larisoft
You are the LAW on the this programming section grin
Re: Programming Interview Test by Nobody: 3:20pm On May 16, 2016
^^^^ #fact, especially now wey i don relegate for top 500 programmers on nairaland chart (last i checked).
Re: Programming Interview Test by Nobody: 3:37pm On May 16, 2016
^^^
You've been too busy with the almighty apps you are working on, but don't worry as your are still the OGA on this section.
Re: Programming Interview Test by larisoft: 4:00pm On May 16, 2016
Febup:
@larisoft You are the LAW on the this programming section grin
lol...thanks, bro.
Re: Programming Interview Test by Nobody: 4:21pm On May 16, 2016
Re: Programming Interview Test by Nobody: 4:31pm On May 16, 2016
Anoda troll on this section?
Re: Programming Interview Test by Nobody: 4:42pm On May 16, 2016
Febup:
@larisoft
You are the LAW on the this programming section grin
my oga dhtml18 transcends him....
Re: Programming Interview Test by Nobody: 5:23pm On May 16, 2016
crotonite:
my oga dhtml18 transcends him....
Pin.chur or adonbilivit
Re: Programming Interview Test by Nobody: 5:45pm On May 16, 2016
dhtml18:

Pinchur or adonbilivit

lol

(1) (2) (3) (Reply)

Tried To Hire A Programmer In Lagos Today He Told Me $100 A Day Or Forget / Programming Challenge / .

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 25
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.