Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,648 members, 7,809,447 topics. Date: Friday, 26 April 2024 at 09:47 AM

Programming Interview Test - Programming (3) - Nairaland

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

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

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

Re: Programming Interview Test by Nobody: 10:40am On May 26, 2016
jacob05:

use .. Repl.it - Python 2

Tried your code works fine but does not allow for P == 0 or P == N-1 as mentioned in the instruction:
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
Re: Programming Interview Test by jacob05(m): 10:43am On May 26, 2016
Febup:


Tried you code works fine but does not allow for P == 0 or P == N-1 as mentioned in the instruction:
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.

It's just an implementation of crotonite's solution in Python ... not entirely mine... undecided undecided
Re: Programming Interview Test by Nobody: 1:50pm On May 26, 2016
jacob05:

It's just an implementation of crotonite's solution in Python ... not entirely mine... undecided undecided

I think his code is ok, but I cannot enter P = values to check his code.
Re: Programming Interview Test by Nobody: 8:01pm On May 26, 2016
cheesy grin cheesy guyz, i finaly got it 100% cool cool

here is my code...

class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A == null || A.length <= 0 || A.length > 100000)
return -1;
long r = 0;

for(int i : A)
r += i;

long l = 0;

for(int p = 0; p < A.length; p++){
r -= A[p];
if( r == l)
return p;

l += A[p] ;
}
return -1;
}
}


thanks @febup for this challenge.

1 Like

Re: Programming Interview Test by Nobody: 8:54pm On May 26, 2016
crotonite:
cheesy grin cheesy guyz, i finaly got it 100% cool cool

here is my code...

class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A == null || A.length <= 0 || A.length > 100000)
return -1;
long r = 0;

for(int i : A)
r += i;

long l = 0;

for(int p = 0; p < A.length; p++){
r -= A[p];
if( r == l)
return p;

l += A[p] ;
}
return -1;
}
}


thanks @febup for this challenge.

You can now have the crown until the next challenge.

1 Like

Re: Programming Interview Test by jacob05(m): 9:03pm On May 26, 2016
crotonite:
cheesy grin cheesy guyz, i finaly got it 100% cool cool

here is my code...

class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if(A == null || A.length <= 0 || A.length > 100000)
return -1;
long r = 0;

for(int i : A)
r += i;

long l = 0;

for(int p = 0; p < A.length; p++){
r -= A[p];
if( r == l)
return p;

l += A[p] ;
}
return -1;
}
}


thanks @febup for this challenge.
Nice.

In Python


def solution(data ) :
if not type(data ) is list or len(data ) <= 0: return -1
lSum = 0; r = sum(data )
for p in xrange(0, len( data ) ) :
r -= data[p]
if (r == lSum ) : return p
lSum += data[p]
return -1
Re: Programming Interview Test by Nobody: 9:10pm On May 26, 2016
jacob05:

Nice.

In Python


def solution(data ) :
if not type(data ) is list or len(data ) <= 0: return -1
lSum = 0; r = sum(data )
for p in xrange(0, len( data ) ) :
r -= data[p]
if (r == lSum ) : return p
lSum += data[p]
return -1

WOW...! python seems to be more concise than java. i think i need to learn python when i master enough of java.

1 Like

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

Weekend Challenge For Practice: RLE Encoding / Sine Wave Inverter Using Arduino / Tried To Hire A Programmer In Lagos Today He Told Me $100 A Day Or Forget

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