78 Interview questions with answers: Part 1

All the mighty buddies who were hit by the recession, just like me, would have definitely gone through the pains of googling and finding out the “78 ques.” list without any answers. I simply hated the guy for compiling the ques. only. I am a no-brainer and like to have an answer before me, just in order to make life simpler.

So here it is, my compilation of the answers along with the question. Almost none of the answers are mine, they are simply copied from various forums/discussions. Njoi!

1.  Write an efficient C program to count the number of bits set in an
unsigned integer.
i/p     o/p
====    ===
0(00)    0
1(01)    1
2(10)    1
3(11)    2
…..  …

ANS:
n=nAND(n-1); count++
if n==0 break;

2.  Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is 1.c
Then, while compiling
$ cc -o 1 1.c
int main()
{
printf(“Hello World\n”);
}
^D
$ ./1
Hello World
$

ANS:
#include “/dev/stdin”

3.  Given a string s1 and a string s2, write  a snippet to say whether s2 is a
rotation of s1 using only one call to strstr routine?
(eg given s1 = ABCD and s2 = CDAB, return  true,
given s1 = ABCD, and s2 = ACBD , return false)

ANS:
condition 1: s1.length() == s2.length()
condition 2: (s1+s1).matches(s2)

4.  What’s the  “condition” so that the following code
snippet  prints both HelloWorld !  if  “condition”
printf (“Hello”);
else
printf(“World”);

ANS:
printf(“Hello”) !=5

5.  Ugly numbers are numbers whose only prime factors are 2, 3 or 5.
The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
shows the first 11 ugly numbers.
By convention, 1 is included.  Write a program to find and print the 1500’th ugly number.

ANS:
int main(){
int n,count = 0,i=1,temp;
printf(”Enter a number\t:”);
scanf(”%d”,&n);
while(count < n){
temp = i;
while(!(temp%2)) temp /= 2;
while(!(temp%3))temp /= 3;
while(!(temp%5)) temp /= 5;
if(temp==1){
++count;
}
else if(i==1){
++count;
}
if(count == n){
printf(”%d”,i);
}
i++;
}
getch();
}

6.  Write a C program which when compiled and run, prints out a message
indicating whether the compiler that it is compiled with, allows /* */
comments to nest.

ANS:
/*/*/0*/**/1

7.  Write a C function that will print 1 to N one per each line on the stdout
where N is a int parameter to the function. The function should not use
while, for, do-while loops, goto statement, recursion, and switch
statement.

ANS:
1. ” if statement is not banned”
int i=1;
/* and then write out INT_MAX times: */
if (i<=N) {
printf(“%d\n”,i);
i++;
}

2. if 1 is unaccepted, recursion
int nprint(int n)
{
if (n == 1) {
return 1;
} else {
printf(“%d\n”, 1 + nprint(n – 1));
return n;
}
}

3. if that too is unaccepted, i think only this is a soln.
void nprint(int n)
{
char output[] = “1\n2\n3\n … 32765\n32766\n32767\n”;
char strn[6], *cp;

sprintf(strn, “%d”, n);
cp = strstr(output, strn);
*(cp + strlen(strn) + 1) = ”;

fputs(output, stdout);
}

8.  int main()
{
int i, n = 20;
for (i = 0; i < n; i–)
printf(“*”);
return 0;
}Change/add only one character and print ‘*’ exactly 20 times.
(there are atleast 3 solutions to this problem 🙂

ANS:
1)for (i = 0; -i < n; i–)
2)for (i = 0; i + n; i–)
3)for (i = 0; i < n;n–)

9.  You are given have a datatype, say X in C.  The requirement is to get the size of the datatype, without declaring a
variable or a pointer variable of that type,  And, of course without using sizeof operator !

ANS
#define size(type) ( (int)((type *)0+1) – (int)((type *)0))

10.  Given a singly-linked, find out the mid point of a single linked list in a single parse of the list. Assume the program would be loaded in read-only memory so no manipulation of the list is allowed.

ANS:
Take two pointers pointing to head of the linked list. Traverse the list using 1st ptr by increasing 1 node at time and 2nd ptr by increasing 2 nodes at a time. When the 2nd ptr reach end of the list, the current position of 1st ptr is the mid point of the given linked list.

Now many of you might hate having the answers just below the questions, because your mighty brains are not given proper chance to get into action. Well then you should go to this link for only questions.

Advertisements

2 thoughts on “78 Interview questions with answers: Part 1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s