Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,209,071 members, 8,004,767 topics. Date: Sunday, 17 November 2024 at 06:34 AM

Programming Interview Test - Programming (2) - Nairaland

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

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

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

Re: Programming Interview Test by Nobody: 12:04pm On May 17, 2016
febup here is my solution.
i implemented the function solution(...) to return an int array of p's. plz check it out!

Re: Programming Interview Test by Nobody: 3:37pm On May 17, 2016
@Crotonite

Very nice of you for doing this test. What you can do now is to add a timer at the start and end of your code to see how long it will take to return all the equilibrium indices of P if N = 100,000. Then rewrite your code to make it run faster using Big 0 Notation if needed.
Re: Programming Interview Test by Nobody: 4:55pm On May 17, 2016
check this one out, it is quite fast (50 iterations of N = 100000 takes under 3secs i.e including the overhead of printing to stdout).

Re: Programming Interview Test by Nobody: 5:31pm On May 17, 2016
@Crotonite

Please try your code on their page at this link: https://codility.com/demo/take-sample-test
I don't think I can grade your code as the test belongs to the website owner at the above link.
Re: Programming Interview Test by Nobody: 5:58pm On May 17, 2016
k grin
Re: Programming Interview Test by Nobody: 2:47pm On May 24, 2016
crotonite:
k grin

I went through your code, what I found was that you have prefixed the values of the array N as: int[] ar = {-1, 3, -4, 5, 1, -6, 2, 1} which was just an example they used in the test. You were meant to generate random integers within the range [−2,147,483,648..2,147,483,647].

You also have this loop: for (int j = 0; j < 50; j++) in which you are calling this function int[] result = solution(arr2, arr2.Length);

This means your code is not doing what is required.

1 Like

Re: Programming Interview Test by Nobody: 2:58pm On May 24, 2016
@febup i am still newbie you know. wink tanx, i will try to fix that asap.
Re: Programming Interview Test by Nobody: 2:59pm On May 24, 2016
@crotonite

Nice try bro keep it up

1 Like

Re: Programming Interview Test by jacob05(m): 3:51pm On May 24, 2016
larisoft:



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;
}
Can someone explain how this runs in O(n)
cc Febup
Re: Programming Interview Test by Nobody: 8:43pm On May 24, 2016
jacob05:

Can someone explain how this runs in O(n)
cc Febup

@larisoft
You need to come and explain your code, I have converted it to C# below, when I run it, it does nothing.

using System;

namespace larisoftBigO
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[8];

solution2(array, cool;
}

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)
{
Console.WriteLine(i.ToString());
Console.Read();
return i;
}

left_sum += nums[i];
i++;
Console.Read();
}

Console.Read();
return -1;

}

private static int Sum(int[] arr)
{

int a = 0;
foreach(int i in arr)
{
a += i;
}
return a;
}
}
}
Re: Programming Interview Test by Nobody: 7:18am On May 25, 2016
@Crotonite
Your code is the closest to the solution, you have now dethroned larisoft unless he can come up with a better solution than yours.
Re: Programming Interview Test by Nobody: 9:12am On May 25, 2016
^^^ah, i don't like what i am hearing at all. Larisoft, oya dethrone crotonite back, who the heck does he think he is?
He should be thankful that i have retired from programming to hunting for monitor lizards (so that i can make frontpage)
Re: Programming Interview Test by Nobody: 9:30am On May 25, 2016
dhtml18:
^^^ah, i don't like what i am hearing at all. Larisoft, oya dethrone crotonite back, who the heck does he think he is?
He should be thankful that i have retired from programming to hunting for monitor lizards (so that i can make frontpage)

boss, make i sit for ur right hand o jare!
Re: Programming Interview Test by Nobody: 9:36am On May 25, 2016
the prototype of my recent solution:


 import java.util.*;

public class Main
{
public static void main(String[] args)
{
//int[] data = {-1, 3, -4, 5, 1, -6, 2, 1};

int[] data = new int[100000];
Random rand = new Random();

for(int i = 0; i < data.length; i++){
data[i] = rand.nextInt();
//System.out.println(data[i]);
}

System.out.println("please wait..."wink;
double start = System.currentTimeMillis();
int equill = solution(data, data.length);

System.out.println();

System.out.println("equillibrum at : " + equill);


double stop = System.currentTimeMillis();

System.out.println("it took " + ((stop - start)/1000) + "seconds to run " );
}





private static int solution(int[] data, int n)
{
long lSum = 0, rSum = 0; //left and right summ of array
int p = 0; //index of suspected equillibrum of array

int ret = -1;

//sum array
for (int i : data)
rSum += i;

//System.out.println("sum.." + rSum);


//if first element is an aquillibrum, add to array
if (rSum - data[p] == 0)
{

return p; }

lSum = data[0];
for (int i = 0; i < n - 1; i++)
{
p++;

//System.out.println("rsum: " + rSum + " lsum: " + lSum);

rSum -= (data[p] + lSum);

if (lSum == rSum)
{

return p;

}//endif

//increment leftsum
lSum += data[p];
}

return ret;
}
}
Re: Programming Interview Test by Nobody: 10:06am On May 25, 2016
Chai, is that language above JAVA? before me i no fit read am, it is too concentrated for me. Abi na objective Java Language?
Re: Programming Interview Test by Nobody: 10:09am On May 25, 2016
codility verified my algorithm....

Re: Programming Interview Test by Nobody: 10:16am On May 25, 2016
Santa maria! i don see new one again o!! Wia is larisoft make him come debug this your codibility nonsense because adonbilivit!!!
Re: Programming Interview Test by Nobody: 10:27am On May 25, 2016
dhtml18:
Chai, is that language above JAVA? before me i no fit read am, it is too concentrated for me. Abi na objective Java Language?
angry


it is java ooo...
Re: Programming Interview Test by Nobody: 10:53am On May 25, 2016
Hmn, this your JAVA is so concentrated that e no be like that JAVA wey i sabi again.
Re: Programming Interview Test by Nobody: 10:56am On May 25, 2016
crotonite:
codility verified my algorithm....

Now let me verify it too from my end.
Re: Programming Interview Test by Nobody: 11:15am On May 25, 2016
Febup:


Now let me verify it too from my end.

cry ooops, me bad!

i did not test for cases like: array == null, array.lenght >100000 || <= 0, etc. i will hv to enhance my algorithm when chanced. thanks...
Re: Programming Interview Test by Nobody: 11:16am On May 25, 2016
dhtml18:
Hmn, this your JAVA is so concentrated that e no be like that JAVA wey i sabi again.
code obfuscation at work sire.
Re: Programming Interview Test by Nobody: 11:31am On May 25, 2016
^^^Interesting. . .
Re: Programming Interview Test by jacob05(m): 11:46am On May 25, 2016
crotonite:
the prototype of my recent solution:


 import java.util.*;

public class Main
{
public static void main(String[] args)
{
//int[] data = {-1, 3, -4, 5, 1, -6, 2, 1};

int[] data = new int[100000];
Random rand = new Random();

for(int i = 0; i < data.length; i++){
data[i] = rand.nextInt();
//System.out.println(data[i]);
}

System.out.println("please wait..."wink;
double start = System.currentTimeMillis();
int equill = solution(data, data.length);

System.out.println();

System.out.println("equillibrum at : " + equill);


double stop = System.currentTimeMillis();

System.out.println("it took " + ((stop - start)/1000) + "seconds to run " );
}





private static int solution(int[] data, int n)
{
long lSum = 0, rSum = 0; //left and right summ of array
int p = 0; //index of suspected equillibrum of array

int ret = -1;

//sum array
for (int i : data)
rSum += i;


//System.out.println("sum.." + rSum);


//if first element is an aquillibrum, add to array
if (rSum - data[p] == 0)
{

return p; }

lSum = data[0];

for (int i = 0; i < n - 1; i++)
{
p++;

//System.out.println("rsum: " + rSum + " lsum: " + lSum);

rSum -= (data[p] + lSum);

if (lSum == rSum)
{

return p;

}//endif

//increment leftsum
lSum += data[p];
}


return ret;
}
}

Is the function solution not running at O(2n) cc dhtml18 Febup.
Re: Programming Interview Test by jacob05(m): 11:51am On May 25, 2016
I guess they grow "linearly" the same way.. so the solution might be valid.
Re: Programming Interview Test by Nobody: 11:55am On May 25, 2016
I agree with the poster above me. . . .
Re: Programming Interview Test by Nobody: 12:00pm On May 25, 2016
jacob05:
Is the function solution not running at O(2n) cc dhtml18 Febup.

It is Linear 0(N)
Re: Programming Interview Test by jacob05(m): 11:46pm On May 25, 2016
crotonite:
febup here is my solution.
i implemented the function solution(...) to return an int array of p's. plz check it out!

In Python


def solution(data):
lSum = 0; ret = []; rSum = sum(data )
if (rSum - data[0] == 0 ) : ret.append(1 )
lSum = data[0]
for p in xrange(1, len( data ) ) :
if (lSum == (rSum - (data[p] + lSum)) ) : ret.append(p)
lSum += data[p]
return ret

ar = [-1, 3, -4, 5, 1, -6, 2, 1 ]
result = solution(ar )
print result
Re: Programming Interview Test by Nobody: 9:55am On May 26, 2016
^^^
I don't have PHP setup on my machine so I tried your code on their site but it gave the error message below:

PHP Parse error: syntax error, unexpected '=' in func.php on line 6
Parse error: syntax error, unexpected '=' in func.php on line 6
RUNTIME ERROR (tested program terminated unexpectedly)

Detected some errors.

/* Your code*/
function solution($A) {
// write your code in PHP5.5
lSum = 0;
ret = [];
rSum = sum(A) )
if (rSum - A[0] == 0 ) : ret.append(1 )
lSum = A[0]
for p in xrange(1, len( A ) ) :
if (lSum == (rSum - (A[p] + lSum)) ) : ret.append(p)
lSum += A[p]
return ret
}
Re: Programming Interview Test by jacob05(m): 9:57am On May 26, 2016
jacob05:

In Python

def solution(data):
lSum = 0; ret = []; rSum = sum(data )
if (rSum - data[0] == 0 ) : ret.append(1 )
lSum = data[0]
for p in xrange(1, len( data ) ) :
if (lSum == (rSum - (data[p] + lSum)) ) : ret.append(p)
lSum += data[p]
return ret
ar = [-1, 3, -4, 5, 1, -6, 2, 1 ]
result = solution(ar )
print result
Febup:
^^^
I don't have PHP setup on my machine so I tried your code on their site but it gave the error message below:

PHP Parse error: syntax error, unexpected '=' in func.php on line 6
Parse error: syntax error, unexpected '=' in func.php on line 6
RUNTIME ERROR (tested program terminated unexpectedly)

Detected some errors.

/* Your code*/
function solution($A) {
// write your code in PHP5.5
lSum = 0;
ret = [];
rSum = sum(A) )
if (rSum - A[0] == 0 ) : ret.append(1 )
lSum = A[0]
for p in xrange(1, len( A ) ) :
if (lSum == (rSum - (A[p] + lSum)) ) : ret.append(p)
lSum += A[p]
return ret
}

Attention to details ... It's in Python 2
Re: Programming Interview Test by Nobody: 10:12am On May 26, 2016
jacob05:
Attention to details ... It's in Python 2

Edited

Ok I will use use .. Repl.it - Python 2 as you have suggested
Re: Programming Interview Test by jacob05(m): 10:16am On May 26, 2016
Febup:


I need someone to run your code for us. Over to you dhtml18 if you have some time to spare.

use .. Repl.it - Python 2

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

Where Can I Buy Programming Books In Lagos / C Programming Challenge To Be Solved Under 10 Minutes / Error With XHTML Markup

(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. 41
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.