## Adobe Interview Question for Software Engineer / Developers

• 0

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

<pre lang="" line="1" title="CodeMonkey28475" class="run-this">/*
Three assumptions not mentioned in the original question.
1. M must be not larger than N.
2. j must be high enough to hold M inside N.
3. i can not be any number, but derived from M and j.
*/

public class BinarySequenceImplant {

public static int implant(int n, int m, int startingPos) {
assert n >= m : "m can not be larger than n";
int msbInM = (int) Math.ceil(Math.log10(m) / Math.log10(2) + 0.001);
assert startingPos >= msbInM : "Starting position is too low to implant m";
int mask = (1 << msbInM) - 1 << startingPos - msbInM;
return n & (~mask) | m << startingPos - msbInM;
}

public static void main(String[] args) {
Random rm = new Random();
int n = 10000000 + rm.nextInt(10000000);
int m = 500 + rm.nextInt(500);
int startingPos = 15;
int shift = startingPos
- (int) Math.ceil(Math.log10(m) / Math.log10(2) + 0.001);
int res = implant(n, m, startingPos);
System.out.println(" n: "
+ String.format("%32s", Integer.toBinaryString(n)));
System.out.println(" m: "
+ String.format("%" + (32 - shift) + "s",
Integer.toBinaryString(m)));
System.out.println("result: "
+ String.format("%32s", Integer.toBinaryString(res)));
}
}

</pre>

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

n|(m<<2)

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

It wont work unless n does not have any set bit withing the segment to be replaced by m.

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

or all bits in m are set.

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

Solution is absolutely correct Provided: i<=j...
Solution could be written as "n|(m<<i)

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

r u sure n|(m<<i) solution is correct suppose N = 101010101010 M=0000 i=1 j=4 ...o/p=101010101010 ... and ans should be 101010100000

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

this works for 8 bit extrapolate for 32.

int BitWise(unsigned char N , unsigned char M, int i , int j)
{
unsigned char m1,m2;

m1 = M;

m1 = m1 >> (8 - j) ;
m1 = m1 << j;
m1 = m1 >> i;

m2 = 0xFF >> (8 - i );
m2 = 0xFF << (8 -i );
m2 = m2 | m1;
unsigned char temp = (0xFF >> j );
m2 = m2 | temp;

unsigned char soln;
soln = ((N | m1) & m2);

return 0;
}

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

int updateBits(int n, int m, int i, int j) {
int max = ~0; /* All 1’s */

// 1’s through position j, then 0’s
int left = max - ((1 << j) - 1);

// 1’s after position i
int right = ((1 << i) - 1);

// 1’s, with 0s between i and j
int mask = left | right;

// Clear i through j, then put m in there
return (n & mask) | (m << i);
}

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

// 1’s, with 0s between i and j
"int mask = left | right;"

Is the same solution from the book "Cracking the Coding Interview", but I think it will always return the biggest number between "left" and "right", no 1's with 0s between i and j.

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

int setbits (uint32_t M, uint32_t N, uint8_t i, uint8_t j)
{
if (0 <(M <<(i-j+1)))
exit

return ( ( ( (~(~0 << i)) & (~0 << j) ) | (M << j) ) | ( (~ ( (~(~0 << i)) & (~0 << j) ) ) & N ) )

}

I think this can work.....

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

M=M<<i; //j doesnt have significance
N=M | N;

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

i think this should work...
m = m << i;
n = n | m;

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

//Changed N with M
N_orig = N;
while (N)
{
N = N >> 1; clz++;
}
N = N_orig;
clz = (1 << clz) - 1;
clz = clz << i;
clz = ~clz;
M = M & clz;
N = N << i;
M = M | N;

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

k=i;
while(k<=j)
N&=~(1<<k);

N=N|(N<<i);

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

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

int main()
{
int N = 2730;
int M = 0;
int i = 1, j = 4;
int answer = N & ~(((1 << (j - i + 1)) - 1) << i);
}``````

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

``````public static void converter(int n, int m, int ii, int jj){
String s = Integer.toBinaryString(n);
int length = s.length();
int max = 0;
for(int i=0; i<length; i++){
max = (int) (max + (1 * Math.pow(2, i)));
}
m = m&max;
int left = (max << jj)&max;
int right = (max << (ii-1))&max;
int x = (left^right)&max;
int xneg = (x ^ max)&max;
int a = (n & xneg)&max;
int b = (m & x)&max;
int result = (a | b);
String sres = Integer.toBinaryString(result);
System.out.print("\nthe binary representation of after merging is : "+sres);``````

}

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

(N>>(i+j))<<(i+j)|M<<i|N&(1<<i - 1)

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

public static int updateBits(int n, int m, int i, int j) {
int max = ~0; /* All 1’s */

// 1’s through position j, then 0’s
int left = max - ((1 << (j+1)) - 1); // here should be j+1 not j.

// 1’s after position i
int right = ((1 << i) - 1);

// 1’s, with 0s between i and j
int mask = left | right;

// Clear i through j, then put m in there
return (n & mask) | (m << i);
}

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

int converter(int m,int n, int i, int j)
{
while (i==j)
{
m & ~i;
i++;
}
n=n<<i;
m=m|n;
return m;
}

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

``````final int n = 1024; // = 10000000000
final int m = 19;   // = 10011
final int i = 2;
final int j = 6;

int mask = (m | (m - 1)) << 2;

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

``N = N | (M << i);``

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

int subNum (int N, int M, int i, int j)
{
while (i <= j)
{
mask = ~ (1 << i) ;
M = ~ M & (1 << i);
N = N & mask ;
i++;
}
N = N | M;
return N;
}

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

``````int subNum (int N, int M, int i, int j)
{

while (i <= j)
{
mask = ~ (1 << i) ;
M = ~ M & (1 << i);
N = N & mask ;
i++;
}
N = N | M;
return N;
}``````

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

#include<stdio.h>

int main()
{

int value = 1024; // 10000000000
int value1 = 21; // 10101
int reult = value | value1 <<2; // 10001010100

printf("%d", reult);
}

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

int subNum (int N, int M, int i, int j)

``````int mask;

while (i <= j)
{
mask = ~ (1 << i) ;
M = ~ M & (1 << i);
N = N & mask ;
i++;
}
N = N | M;
return N;``````

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

#include<stdio.h>

int main()
{

int value = 1024; // 10000000000
int value1 = 21; // 10101
int reult = value | value1 <<2; // 10001010100

printf("%d", reult);
}

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.