Thursday, August 09, 2007

"C" the Bug - Part I

1. Find the bug.

All necessary Headers included like stdio.h, stdlib.h, string.h etc.

int main()
{
char *str = NULL;
int i = 1;
str = (char*)malloc(sizeof(char)*2);
if(!str) {
printf("Malloc Failed\n");
exit(1);
}
strcpy(str, "Hi");
printf("%c\n", str[i*i+i/i]);
str = (char *)realloc(str, sizeof(char)*20);
if(!str) {
printf("Realloc Failed\n");
exit(1);
}
strcpy(str, "Hello World\n");
i = 2;
printf("%c\n", str[i*i+i/i]);
return 0;
}

2. In a doubly linked list, the nodes are paired and these pairs have to be reversed.

For Ex: If the linked is as below,
1--2--3--4--5--6
Output should be:
5--6--3--4--1--2

Struct is as below:
typedef struct node {
int data;
struct node *next;
struct node *prev;
} NODE;

Handle all the conditions. This question was asked in Microsoft Interview.
Deadline is on 18th Aug 2007.
Send it to me at arvind.bhushan@gmail.com or post in the comment.

Challenging the Code
--------------------
* You need to have trust in me.
* Specify in your mail or comments as "Challenge".
* Rs.50/person.
* Max. challenge amount = Rs.(No. of Persons challenging * 50). Conditions Apply.
* If your code breaks under any conditions, you will lose the amount.
* If your code works smooth, you will win the Total Challenged amount.
#Note#
The challenge amount increases if anyone supports me. Specify in your mail or comments as "Support". For those supporting me will definitely get a share if someone loses the challenge.

If not interested in challenge or support, you can also mail or post comment as "Fun".

No comments: