Examine the following return statement, does it make sense?
return count++;
Examine the following conditional expression, what might be wrong?
if (mask & ASYNC_BIT == 0x0100) {....
Here is a link to a sample of Microsoft's interview questions on the C language. It has some brain teaser for you.
What is hashing? How can it be used? What is the advantage of using this data structure? What is the drawback of this technique? Write simple code to do 'lookup', 'remove' and 'insert' operations using a hash chain data structure. If mutual exclusion is required in the multi-threaded environment, what considerations and modifications do you need to change the code for lookup, remove and insert operations?
Read the following code fragments and comment on the following items:
1) describe what is "writepage" in the struct address_space_operations.
2) describe what is nfs_file_aops and what are the : and , in C syntax.
3) comment on the general flow of the function nfs_writepage.
struct address_space_operations {
int (*writepage)(struct page *);
int (*readpage)(struct file *, struct page *);
int (*sync_page)(struct page *);
int (*prepare_write)(struct file *, struct page *, unsigned, unsigned);
int (*commit_write)(struct file *, struct page *, unsigned, unsigned);
};
struct address_space_operations nfs_file_aops = {
readpage: nfs_readpage,
sync_page: nfs_sync_page,
writepage: nfs_writepage,
prepare_write: nfs_prepare_write,
commit_write: nfs_commit_write
};
/*
* Write an mmapped page to the server.
*/
int
nfs_writepage(struct page *page)
{
struct inode *inode;
unsigned long end_index;
unsigned offset = PAGE_CACHE_SIZE;
int err;
struct address_space *mapping = page->mapping;
if (!mapping)
BUG();
inode = mapping->host;
if (!inode)
BUG();
end_index = inode->i_size >> PAGE_CACHE_SHIFT;
/* Ensure we've flushed out any previous writes */
nfs_wb_page(inode,page);
/* easy case */
if (page->index < end_index)
goto do_it;
/* things got complicated... */
offset = inode->i_size & (PAGE_CACHE_SIZE-1);
/* OK, are we completely out? */
err = -EIO;
if (page->index >= end_index+1 || !offset)
goto out;
do_it:
if (!PageError(page) && NFS_SERVER(inode)->rsize >= PAGE_CACHE_SIZE) {
err = nfs_writepage_async(NULL, inode, page, 0, offset);
if (err >= 0)
goto out_ok;
}
err = nfs_writepage_sync(NULL, inode, page, 0, offset);
if ( err == offset) {
out_ok:
err = 0;
}
out:
UnlockPage(page);
return err;
}
The following is a collection from an unknown company.
1. Consider the following structure definitions:
typedef struct {
int a;
int b;
int c;
} ABC;
typedef struct {
int d;
int e;
int f;
ABC *abc;
} DEF;
Write code for the following two functions:
// The create function uses a SINGLE MALLLOC to allocate for structure
// DEF and its child structure (i.e. at the end of the allocation,
// these two lines of code will be valid:
//
// DEF *pdef = CreateDEF ();
// pdef->abc->a = 10;
DEF *CreateDEF (void)
{
}
// Note: To the free function, we are passing a pointer to ABC.
void FreeDEF (ABC *pabc)
{
}
2. Is the following code OK? What does it do?
DEF *pdef = CreateDEF();
int value;
value = (int) pdef;
value += 8;
*(int *) value = 10;
3. Consider 4 components of a color where:
unsigned char red = 0x10;
unsigned char green = 0xFF;
unsigned char blue = 0x0F;
unsigned char alpha = 0x44;
Generate a packed color ARGB which is a 32 bit integer such that A is in the
MSB of ARGB followed by red, green, blue
-----------------
| A | R | G | B |
-----------------
unsigned int PackBytes (unsigned char red, .. green, ... blue, .. alpha)
{
}
4. What is wrong with the following code?
a. How would you fix it by changing the code?
b. How would you fix it by changing the macro? (Not allowed to
collapse the three printf's into a single printf).
#define PRINT_THREE_TIMES \
printf ("one: %d\n", 1); \
printf ("two: %d\n", 2); \
printf ("three: %d\n", 3); \
if (i == 1)
....;
else if (i == 2)
....;
else if (i == 3)
PRINT_THREE_TIMES
else if (i == 4)
....;
5. Consider the following code:
int CheckForZero (int a[], int n)
{
int i;
for (i = 0; i < n; i++) {
if (a[i] == 0) {
return (TRUE);
}
}
return (FALSE);
}
This code works - but it does a check for every element in 'a' - i.e.
it does "n" compares and bails early if it finds even one zero element.
Our array 'a' generally does not have a zero. Optimize this code to
do only one compare. Feel free to instead use some mathematical
operations such as ADD, SUB etc.
6. Consider the following function:
int SpecialFunction (int a)
{
if (a <= 1) {
return (1);
} else {
return (a * SpecialFunction (a - 2));
}
}
What is the value of x where "x = SpecialFunction (11)"
7. Write code to see if a number is a power of 2
int Is2Power (unsigned int a)
{
}
8. What is the value of x where "x = sizeof (struct A)" on a 32 bit machine?
Show your calculations.
struct A {
char a;
char b;
int c;
short d;
int *p;
};
9. The operating system typically allocates memory in pages such that
the base address of the pages are 0, 4K, 8K, .... Given two
addresses, write a program to find if two pointers are on the
same page.
int AreOnSamePage (int *a, int *b)
{
}
10. Write a function to generate a nearest integer value.
int round (float f)
{
}