Google Interview Question for Developer Program Engineers


Country: United States
Interview Type: Phone Interview




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

Suppose using Java, unless we need bound check, there's no need for 'len'

void drawLine(byte[] img, int len, int wid, int r, int x1, int x2) {
        if ( x2 < x1 ) {
              logErrorAndExit();
        }
        if ( r > len ) {
              logErrorAndExit();
        }
        // First compute from which bit to start/stop
        int startBit = (r-1) * wid + x1;
        int stopBit = (r-1) * wid + x2;
        // Then compute the corresponding in the array
        int startByte = startBit / 8;
        int stopByte = stopBit / 8;
        // Then compute from which bit of the byte to start/stop
        int byteStartBit = startBit % 8;
        int byteStopBit = stopBit % 8;
        // Then set the bits
        // Set intermediate bytes first
        for ( int i = startByte +1; i < stopByte; i++ ) {
              img[i] = img[i] | 0xFF; // suppose we are using jdk1.7
        }
        // Then set the start/end byte
        for ( int i = 0; i < byteStartBit; i++ ) {
              img[startByte] = img[startByte] | 1 << (8-i);
        }
        for ( int i = 0; i < byteEndBit; i++ ) {
              img[stopByte] = img[stopByte] | 1 << (i);
        }   
}

- flamearrow February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome dude. Only one question/suggestion. I think the following for loop:

// Then set the start/end byte
        for ( int i = 0; i < byteStartBit; i++ ) {
              img[startByte] = img[startByte] | 1 << (8-i);
        }

should be

for ( int i = byteStartBit; i < 8; i++ ) {
              img[startByte] = img[startByte] | 1 << (8-i);
        }

You don't want to set the bits from left to byteStartBit, you want to set from byteStartBit to the end of the byte (going right)

- Anon February 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void drawLine(byte[] img, int len, int wid, int r, int x1, int x2) {
for(int i=wid*r+x1; i<=wid*r+x2;i++) {
 img[i]=1; //in java, we need not worry about of array index out of bounds, as jvm throws this error if i is going out of range of img array last index. Otherwise, we can add a check i<=img.length
}

- ashok February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hey updated the question, each byte corresponds to 8 pixels or each pixel is a bit not a byte

- Phoenix February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

length and width? Don't you mean height and width?

- Anonymous February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I don't get this qn. Can you please clarify with an example? What I make out is, byte[] img is a 2D array whose width is 8 (8 bits). In this 2D array, we have to set x1->x2 in row r as 1. Right? If this is the case, then length and width are not required. My solution for this case would be -

void drawLine(byte[] img, int len, int wid, int r, int x1, int x2) {
 byte[r] = 0; // let x1 = 3, x2 = 5;
 byte[r] << (x2-x1+2); // byte[r] = 00001000
 byte[r] = byte[r] - 1;    // byte[r] = 00000111
 byte[r] = byte[r] << (8-x2); //byte[r] = 00111000
}

- Biru February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class DrawLine {
	
	static void drawLine(byte[] img, int len, int wid, int r, int x1, int x2) {
		int nbyte = wid/8;
		
		printImg(img, wid);

		for (int i = x1; i <= x2; i++) {
			int index = getIndex(i, r, nbyte);
			int bitPos = getBitPos(i, nbyte);
			img[index] = (byte) (img[index] | (0x80 >> bitPos));
		}

		System.out.println();
		printImg(img, wid);
	}
	
	static int getIndex(int x, int row, int nbyte) {
		return (row-1)*nbyte + (x >> 3); 
		
	}
	
	static int getBitPos(int x, int nbyte) {
		return (x - ((x >> 3) << 3));
	}
	
	static void printImg(byte[] img, int wid) {
		int row = img.length * 8 /wid;
		int nbyte = wid/8;
		
		for (int r = 1; r <= row; r++) {
			for (int i = 0; i< wid; i++) {
				int index = getIndex(i, r, nbyte);
				int bitPos = getBitPos(i, nbyte);

				if ((img[index] & (0x80 >> bitPos)) > 0)  System.out.print("1");
				else System.out.print("0");
			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		byte[] img = new byte[32];
		
		drawLine(img, 0, 32, 7, 9, 31);
	}
}

- Happy February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I recently gave google interview, was not able to code in one round, all othr round went good.
Wht r my chances?

- Srikant Aggarwal February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

len/wid parameters mean how many pixels for a horizontal/vertical line?

- shengzhc March 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote

Not necessarily. You're going to use bit manipulation.

- BigD February 01, 2013 | Flag
Comment hidden because of low score. Click to expand.


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