Microsoft Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
This question is quite vague and I'm not sure if I got it right.
You have two pointers which point to some locations in the array.
Iterate over the array and find that locations.
Now you have four possibilities:
if ((p1 && p2) < 128) - there are two 'one byte' numbers
if ((p1 && p2) >= 128) - there are two '2 byte' numbers - both pointers can point to first digit
if ((p1 >= 128 && p2 < 127) - there is one '2 byte' number - p1 is first digit
if ((p2 >= 128 && p1 < 127) - there is one '2 byte' number - p2 is first digit
>> one points to the start of the and...
I guess this should be "to the start of the array"
while (p1 < p2)
{
if ((arr[p1] & 0x80) == 0)
{
p1 += 1;
}
else
{
p1 += 2;
}
}
if (p1 == p2)
{
if ((arr[p2] & 0x80) == 0)
{
// p2 points to a single-byte char: arr[p2]
}
else
{
// p2 points to a double-byte char: arr[p2], arr[p2+1]
}
}
else
{
// p2 points in the middle of a double-byte char.
}
Move the first pointer to just the byte before the second pointer.
Now check the value. If it's between 0 to 127 then either way it is the last byte. Hence if the second pointer is > 128 then it is the first byte of the two byte word else is the one byte word.
if the value of first pointer is >128 then the second pointer is definitely the second byte of the two byte word.
Iterate over the array elements, and decode the type of the current element:
- ranan.fraer May 17, 2014- if in the range of [0,127] then it's the first byte of a one byte character
- if in the range of [128,255] then it's the first byte of a two byte character. Move to the next array element, and mark it as the second byte of a two byte character.
Stop when the current pointer equals the pointer that we search for and report the type found.