Google Interview Question
Software Engineer / DevelopersTeam: Youtube
Country: United States
Interview Type: Phone Interview
This will not work for 00 00 00 03. So the transition happens as follows:
S0 -> 00 -> S1
S1 -> 00 -> S1
S1 -> 03 -> S2
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 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.
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;
}
@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!
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 ?
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;
}
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] + " ");
}
}
}
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();
}
}
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