Sunday, July 12, 2009

Intel interview question II

1. Write a function that will return the bit mirror image of inputData. (i.e. 11110010b => 01001111b)
unsigned short mirror (unsigned short inputData)
{

int numberByte = sizeof(unsigned short);//decide how many bytes in this processor.
int *temp_buffer,j,temp;
unsigned short mirrorOutput=0;
int numberBits = numberByte * 8; // how many bits for inputData
// allocate the memory
temp_buffer = (int*)malloc(sizeof(int)*numberBits);

// read bits information into temp_buffer.
for (j =0;j temp_buffer[j]= (inputData & (1<
// output the bits informaiton for inputData.
printf("output the bits informaiton for inputData\n");
for (j =0;j printf("%d ",temp_buffer[j]);


// inverse the buffer.
for (j=0;j temp = temp_buffer[j];
temp_buffer[j]= temp_buffer[numberBits-1-j];
temp_buffer[numberBits-1-j] = temp;
}
// output the mirror bits information for outputData
printf("\n\n output the bits informaiton for outputData\n");
for (j =0;j printf("%d ",temp_buffer[j]);

// output the integral number
for (j=0;j mirrorOutput = mirrorOutput + temp_buffer[j]*(1<
return mirrorOutput;
}




2. What is the output from the following code? (Assume 32-bit integers)

int x[] = {1,2,3,4,5};
int *p = x;
char *pc = (char *) p;
*(p++) = 6 ;
*(int *)(pc+4) = 7;
printf(”%d %d %d %d %d\n”, x[0], x[1], x[2], x[3], x[4]);


6,7,3,4,5.





3. What is the value of a 32-bit integer with bits 13:9 (9 through 13) set to 0x1A?
4. Given an array that contains N numbers, write a routine to determine if there are two numbers in the array whose sum equals the number K. For example, given the array {8, 4, 1, 6}, and K=10, then return true since 4+6=10. Describe a solution that would take O(N log N) time.
5. Answer the following questions relating to the code below:
class Buffer {
static int dataBuffer[100];
static int head = 0;
static int tail = 0;

public:
static void Produce(int data) {
dataBuffer[head++] = data;
if (head == 100) head = 0;
}
static bool CanConsume() {
if (head != tail) return true;
return false;
}
static int Consume() {
int pos = tail++;
if (tail == 100) tail = 0;
return dataBuffer[pos];
}
}
void Producer() {
while(1) {
… some stuff …
Buffer.Produce(x);
… some stuff …
}
}
void Consumer() {
while(1) {
… some stuff …
if(Buffer.CanConsume() ) {
int x = Buffer.Consume();
… do something with x …
}
… some stuff …
}
}

a. If we have a multithreaded system with one thread executing ‘Producer’ and one thread executing ‘Consumer’ is there anything wrong with the code above? Give details.
The major bug in this implementation is buffer is not protected. Two problems will occur: the producer may write into the buffer and index is not updated yet, consumer will get the wrong result. We can use the semaphore that limits access to that section of code to only one thread at a time.

b. If we have two threads running ‘Producer’ and one thread running ‘Consumer’ is there anything wrong with the code above? Give details.
If two threads running ‘producer’ and one thread running ‘consumer’, the buffer will be overflow because of the fast speed of producing data. We need another semaphore indicating the buffer is not full in order to produce the data into buffer.


c. If we have one thread Running ‘Producer’ and two threads running ‘Consumer’ is there anything wrong with the code above? Give details.
If we have one thread Running ‘Producer’ and two threads running ‘Consumer’, the buffer will be empty. The thread consumer will wake up, grab the lock and see that the buffer is empty or not, and then go back to sleep. We need set another semaphore indicating the buffer is not empty.



6. Write a function to return a nearest integer value given a float input.

int round (float f)
{
return nearIntegel = f>=0?(int)(x+0.5):(int)(x-0.5);
}



No comments: