## JP Morgan Interview Question for Applications Developers

Country: United States

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

// take 2 binary
var addIt = function(a, b) {
var aI = parseInt(a, 2);
var bI = parseInt(b, 2);

var t = aI + bI;

// convert to binary
t = (t >>> 0).toString(2);
console.log(t);
};

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

``````#include <stdio.h>

#define len(s) sizeof(s)/sizeof(s[0]) - 1
#define MAX(a,b) ((a) > (b)) ? (a) : (b)

void addBitStrings(char *op1, int op1_s, char *op2, int op2_s, int carry, char *result, int result_s){
if (result_s < 0) return;

int first = 0;
int second = 0;

if (op1_s >= 0)
first = op1[op1_s] - '0';
if (op2_s >= 0)
second = op2[op2_s] - '0';

result[result_s] = (first ^ second ^ carry) + '0';
carry = (first & second) | (second & carry) | (carry & first);
addBitStrings(op1, op1_s - 1, op2, op2_s - 1, carry, result, result_s - 1);
}

int main(){
char op1[] = "1100011";
char op2[] = "10";
char *result;
int max = MAX(len(op1), len(op2)) + 1;
result = (char*)calloc(sizeof(char)*max + 1, 1);
printf("%s", addBitStrings(op1, len(op1) - 1, op2, len(op2) - 1, 0, result, max - 1));
return 0;
}``````

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

``````// ZoomBA
str( int(a,2) + int(b,2) , 2 )
}``````

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

/* Of course use StringBuilder instead of just String. Accidentally implemented turning int into String also :D
*/

public class BinaryStringSum {

public static void main(String[] args) {

System.out.println(Integer.valueOf(intToString(32623634),2));
System.out.println(Integer.valueOf(intToString(43442642),2));

System.out.println(Integer.valueOf(binaryStringSum(46263, 43734),2));
System.out.println(46263+43734);

}

public static String binaryStringSum(int a, int b) {

String bS , aS, res = "";
bS = intToString(b);
aS = intToString(a);

int i = bS.length() > aS.length() ? bS.length()-1 : aS.length()-1, c = 0;
while(i >= 0) {
int bit1 = aS.length() > i ? aS.charAt(i) - '0' : 0;
int bit2 = bS.length() > i ? bS.charAt(i) - '0' : 0;
int sum = bit1 + bit2 + c;
c = (2 & sum) != 0 ? 1 : 0;
res = ((sum & 1) != 0 ? 1 : 0) + res;
i--;
}
if(c == 1) res = 1 + res;

return res;
}

public static String intToString(int num) {

int mask = 1 << 30;
String numS = "";

if(num == 0) return "0";
else {
}
numS = numS + ((num&mask)!= 0 ? 1 : 0);
}
}

return numS;
}

}

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

``````char *decimal_to_binary(int n)
{
int c, d, count;
char *pointer;

count = 0;
pointer = (char*)malloc(32+1);

if ( pointer == NULL )
exit(EXIT_FAILURE);

for ( c = 31 ; c >= 0 ; c-- )
{
d = n >> c;

if ( d & 1 )
*(pointer+count) = 1 + '0';
else
*(pointer+count) = 0 + '0';

count++;
}
*(pointer+count) = '\0';

return  pointer;
}

int fromBinary(char *s) {
return (int) strtol(s, NULL, 2);
}

char * sum(char * firstnumber, char * secondnumber){

int firstnum = fromBinary(firstnumber);
int secondnum = fromBinary(secondnumber);
int value = firstnum + secondnum;

return decimal_to_binary(value);
}``````

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

``````public String testbin(String binA, String binB) {
return Integer.toString((Integer.parseInt(binA,2) + Integer.parseInt(binB,2)),2);
}``````

to call would be

``System.out.println(("Result:"+ testbin("111","1"));``

Result: 1000

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

``````String addBinaryNumbers(String xstr, String ystr) {
String result="";
int r=0, x=0, y=0;
int carryover[]=new int[31];
//
for (int k=xstr.length()-1, bit = 0; k>=0; k--, bit++) { if(xstr.charAt(k)=='1') { x+=(int) Math.pow(2, bit); }   }
for (int k=ystr.length()-1, bit = 0; k>=0; k--, bit++) { if(ystr.charAt(k)=='1') { y+=(int) Math.pow(2, bit); }   }
//
for (int bit = 0; bit <31; bit++) {
// cases
if( carryover[bit]==1 ) {
if( is_set(x, bit) && !is_set(y, bit) ) { carryover[bit+1]=1; }
if( !is_set(x, bit) && is_set(y, bit) ) { carryover[bit+1]=1; }
if( is_set(x, bit) && is_set(y, bit) ) { r=set_bit(r, bit); carryover[bit+1]=1; }
if( !is_set(x, bit) && !is_set(y, bit) ) { r=set_bit(r, bit); }
}else{
if( is_set(x, bit) && !is_set(y, bit) ) { r=set_bit(r, bit); }
if( !is_set(x, bit) && is_set(y, bit) ) { r=set_bit(r, bit); }
if( is_set(x, bit) && is_set(y, bit) ) { carryover[bit+1]=1; }
if( !is_set(x, bit) && !is_set(y, bit) ) { }
}
}
result=Integer.toString(r,2);
return result;
}

/** set bit[i] to 1 */
int set_bit(int y, int b) {
return y | (1 << b); // set the bit on, logical OR
}``````

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

``````#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int str_to_int(char *s)
{
int len = strlen(s);
int i = 0;
int res = 0;

while(len--) {
if (s[len] == '1')
res += 1 << i;
i++;
}

return res;
}

char *int_to_bin(int n)
{
int i;
char *res = malloc(33);

if (!res)
return NULL;

res[32] = 0;

for (i = 31; i != -1; i--) {
res[i] = (n & 0x1) ? '1' : '0';
n >>= 1;
}

return res;
}

{
char *s = str;
char *res = malloc(33);

if (!res)
return NULL;

while (*s == '0') {
s++;
}
strcpy(res, s);
free(str);

return res;
}

char *sum_bits(char *s1, char *s2)
{
int a = str_to_int(s1);
int b = str_to_int(s2);
int sum = a + b;
char *res;

if (!sum) {
res = malloc(2);
if (!res)
return NULL;
res[1] = 0;
res[0] = '0';
}

}

int main()
{
char *s = sum_bits("111", "1");
if (!s)
return 1;

printf("%s\n", s);
free(s);

return 0;
}``````

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.

### 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.

### 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.

### 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.