Adobe Interview Question for Software Engineer / Developers






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>

- Anonymous April 29, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

n|(m<<2)

- wtf May 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous May 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

or all bits in m are set.

- Anonymous May 01, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- S May 02, 2011 | Flag
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

- d_mak May 02, 2011 | Flag Reply
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;
}

- Anonymous May 04, 2011 | Flag Reply
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);
}

- Anonymous May 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

- Anonymous November 21, 2011 | Flag
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.....

- Anonymous May 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous May 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

i think this should work...
unsigned int mask = ~(~(~0<<(j-i+1))<<i);
n = n & mask;
m = m << i;
n = n | m;

- beginner June 15, 2011 | Flag Reply
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;

- Amit July 05, 2011 | Flag Reply
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);

- Anonymous August 11, 2011 | Flag Reply
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);
        answer |= M << i;
        printf("%d\n", answer);
}

- Ravi November 16, 2011 | Flag Reply
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);

}

- CAFEBABE December 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Nousi February 22, 2013 | Flag Reply
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);
}

- Anonymous September 28, 2013 | Flag Reply
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;
}

- Rohit Raravi February 23, 2014 | Flag Reply
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;

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

        System.out.println(Integer.toBinaryString(n | mask));

- Alexander Volov December 20, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

N = N | (M << i);

- swapnilsj August 06, 2016 | Flag Reply
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;
}

- ptilloo November 20, 2016 | Flag Reply
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;
}

- ptilloo November 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<stdio.h>


int main()
{

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

printf("%d", reult);
}

- RAMESH December 06, 2020 | Flag
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;

- ptilloo November 20, 2016 | Flag Reply
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);
}

- RAMESH GARIDAPURI December 06, 2020 | Flag Reply


Add a Comment
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.

Learn More

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.

Learn More

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.

Learn More

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.

Learn More