Google Interview Question for Software Engineer / Developers


Team: Youtube
Country: United States
Interview Type: Phone Interview




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

If I understand the question correctly, I think it can be solved with a state machine. State machine has 4 states 00 takes you from S0 to S1 and then from S1 to S2 and 03 takes you to S3. You need a couple of more transitions to cover overlaps. I'm not sure if this is what the interviewer had in mind but this is the best I can do with the description.

- solonor2011 January 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome!

- Anonymous January 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not work for 00 00 00 03. So the transition happens as follows:
S0 -> 00 -> S1
S1 -> 00 -> S1
S1 -> 03 -> S2

- Anonymous January 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another way is to leave a counter:
Initialize counter = 0;
let c = buffer.read(); // put this in a while loop until stream is empty
if (c='03'){
if( counter<2)
add(c) to sync stream;
}
else{
add(c) to sync stream
if(c == 00)
counter++;
else
counter=0;
}

- Anonymous February 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@ [Anonymous on January 17, 2013]:

If we have this state machine then we are done. States (S1, S2, S3 and S4) descriptions are listed below:

S1 : initial state.
S2 and S3 : intermediate states.
S4 : state when pattern 00 00 03 is achieved, here remove 03 from the stream.

INPUT		CURRENT_STATE		NEXT_STATE
 (00)				S1					S2
!(00)				S1					S1

 (00)				S2					S3
!(00)				S2					S1

 (03)				S3					S4
 (00)				S3					S3
(!(00) && !(03))		S3					S1

 (00)				S4					S2
!(00)				S4					S1

If required we could have more state (say Sf) to show final state, i.e., when input stream ends.

- A March 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

He asked me that There is a huge byte stream and I should not cache the entire stream for parsing. I had a very little time and This is what I did

std::vector<unsigned char> temp;
temp.reserve(3);

unsigned char read_sync_byte()
{
  // std::iterator<unisigned char>  it;
   //it = vec.begin();
   unsigned char ret;
      
   temp.push_back(read_byte());
    while(vec.size -1 != 3){
 temp.push_back(read_byte());
       }
    if(vec[2] == ‘03’){
      if(vec[0] == ‘00’ && vec[1]==’00’)
          vec[2] = read_byte();
          }

     ret =vec.begin();
   vec.erase(vec.begin())
   return ret;  
 }

- Yashwanth January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Yashwant, the code part:

if(vec[2] == ‘03’){
	if(vec[0] == ‘00’ && vec[1]==’00’)
        	vec[2] = read_byte(); // we discard current '03' value and read next value.
}

Will also discard all '03' byte values in a stream like this: ... 00 00 03 03 03 03 ...

I doubt if this is allowed/ not allowed to happen as per the description.

Nice try by the way!

- A March 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If we call pattern 00 00 03 "X" then a sequence 00 00 00 00 00 03 can be read as 00 00 00 X can' it. If we start counting zeroes as soon as we find one, and we get at 03 at any given time after our counter is >= 2 then we should be good shouldn't be? Or is there any rule that says that 00 00 03 != 00 00 00 03 ?

- Antonio January 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice idea!

- A March 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Agree with you @solonor2011.

However I am absolutely not sure about the synchronization issue, if any, in this problem.

@Yashwanth: Can you give another try in explaining the problem.

- Learner January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

from wiki: In computer networks, a syncword, sync character or preamble is used to synchronize a transmission by indicating the end of header information and the start of data.

So we need remove such "sync word" from the whole stream.

If I understand the question correctly, the following codes may help :)

unsigned int continue_zero_count = 0;
unsigned char read_sync_byte()
{
unsigned char current_byte = read_byte();
if (0x03 == current_byte && 2 == continue_zero_count)
{
continue_zero_count = 0;
if (0x00 == (current_byte = read_byte())) continue_zero_count++;
}
else if (0x00 == current_byte)
{
continue_zero_count = (coutinue_zero_count + 1 <= 2) ? (continue_zero_count + 1) : (2);
}

return current_byte;
}

- wakensky January 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can someone please the explain the question with an example. I did not understand the question.

- DashDash January 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class ElimString
{
	public static void main(String[] s)
	{
		int[] nr = new int[]
		{1, 4, 5, 6, 4, 0, 0, 3, 1, 3, 4, 0, 0, 0, 0, 3, 5, 6, 7, 8, 9, 9, 0, 0, 3};

		//manipulate the last 3 bits to track the state: 000
		int pattern = 0;
		for (int i = 0; i < nr.length; i++)
		{
			//shift bits left, this adds a 0 by default 
			pattern <<= 1;

			//if this was a non-0 nr, should we skip printing it if it's a 3?
			if (nr[i] != 0)
			{
				//turn that last 0 into a 1
				pattern |= 1;
				//7 in binary is 111, check if the last 3 bits are arranged like 001
				if (nr[i] == 3 && (pattern & 7) == 1) continue;
			}
			System.out.print(nr[i] + " ");
		}
	}
}

- olegmeister January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class BytesStreamReader{
	Deque<Character> buffer;
	BytesStreamReader(){
		buffer = new Deque<Character>();
}
public char read_sync_byte(){
	while(buffer.size() < 3){
		char c = read_byte();
		if(c == null){
			break;
}
		buffer.addLast(c);
}
if(buffer.get(0) == 0 && buffer.get(1) ==  0 && buffer.get(2) == 3){
	buffer.pollLast();
		char c = read_byte();
		if(c != null){
			buffer.addLast(c);
}
}
if(buffer.isEmpty()){
	return null;
}
return buffer.pollFirst();
}

}

- kuroobi October 31, 2013 | 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