Interview Questions

240.There is a byte array which contains the character of one

Microsoft Interview Questions and Answers


(Continued from previous question...)

240.There is a byte array which contains the character of one

Question:
There is a byte array which contains the character of one byte and two bytes. One byte character has range 0 to 127 and first character of 2 byte character is 128 to 255 and second byte character has range 0 to 255. Now, two pointers are given, one points to the start of the and another points to somewhere else. Tell which character 2nd pointers points to?


maybe an answer:


#include <iostream>
using namespace std;

unsigned char findHim(const unsigned char *ptr1, const unsigned char *ptr2)
{
if(ptr1 == ptr2)
{
if( *ptr1 > 127 )
return 1; //first byte of two byte char
else
return 0; //single byte
}
if(ptr1>ptr2)//overtook ptr2 ,,means it was two byte character
{
return 2; //second byte of two byte char.
}

if( *ptr1 > 127 )
return findHim(ptr1+2,ptr2); //first byte of two byte char
else
return findHim(ptr1+1,ptr2);;
}
int main()
{
unsigned char arr[] = {148,149,123,123,149,123,149,150,245,233,122};
const unsigned char *ptr1= arr, *ptr2 = arr+10;
unsigned char retVal = findHim(ptr1,ptr2);
return 0;
}

(Continued on next question...)

Other Interview Questions