Epic Systems Interview Question
All you need to do is check the msb at index-2. Assuming valid input, if MSB@index-2 is 1,then the prev character is kanji.. otherwise its ascii
int backspacePos(int index, char*arr)
{
if(index==0 ||index==1) return 0;
if(arr[index-2]&0x10000000==0x10000000)
return index-2;
return index-1;
}
char* shiftArr(int index1, int index2, char *arr)
{
int t1=index1,t2=index2;
while(arr[t2]!='\0')
{
arr[t1]=arr[t2];
t1++;
t2++;
}
arr[t1]='\0';
return arr
}
//usage
//k=backspacePos(index,array)
//array=shiftArr(k,index,array);
first a few things to note-
--- i am assuming the data is stored as big endian. if it were stored as little endian, it would actually be easier for us to decode. little endian means if the data in hex is - 0xABCD, in the memory it is stored as CD AB. so for Kanji I would check if 0xCD & 0x80 == 1. since I am assuming big endian just for fun, by byte ordering is like AB CD.
--- how do I get the CD byte from the given index.
strncpy(a,&array[index-1],2); //so this will copy 2 bytes from the required position, which is one less than the given position.
if(a[1]&0x80==0x80) we know it was a kanji character, else fro ASCII we need to make sure a[0]& 0x80 == 0, because the MSB for that byte needs to be 0 for it to be an ASCII .
once we have figured out which character it was, it is easy to delete either one byte (ascii) or 2 bytes(kanji) from the original array.
now the original index numbers will be affected and I do not know whether they want us to fix that too.
---
I Hope this was the original question..
First some definitions for this problem: a) An ASCII character is one byte long and the most significant bit in the byte is always ‘0′. b) A Kanji character is two bytes long. The only characteristic of a Kanji character is that in its first byte the most significant bit is ‘1′.
Now you are given an array of a characters (both ASCII and Kanji) and, an index into the array. The index points to the start of some character. Now you need to write a function to do a backspace (i.e. delete the character before the given index).
As it is most obvious, the data structure to be used in this scenario is the STACK.
Here's the pseudocode:
- nagu89 September 20, 2009