Few months back, i have attended the one Product Based Company interview, 2 times within 2 months of time. First time, they have done my interview for firmware development requirement which needs in depth knowledge of memory related issues and C language knowledge related to the same. That is, xpectation was high and hence my profile didnt match. Even though they have done their duty...! With lot of good questions, i was able to get benefited by that interviews.
After 1 month of time, i got once again call for Noida requirement. When i sat infront of the interviewer, he asked me to write one C program to reverse a sentence word wise. That is, if the input is 'I am Pramod', the output should be 'Pramod am I'. When he saw my happy face, he told to write using only one loop and in optimized manner.....!!!!!
I have written as follows.
/****************************************
* 1st Version of the program
*
******************************************/
void main()
{
char str[] = "I am Pramod";
char *temp; int i;
char *ptr[10];
temp = strtok(str, ", \n");
i=0;
while(temp != NULL)
{
ptr[i] = (char*)malloc(20);
strcpy(ptr[i], temp);
temp = strtok(NULL,", \n");
i++;
}
for(i=i;i>=0;i--)
if(ptr[i]!=NULL)
printf("%s\n",ptr[i]);
}
After this, he came to know that i am working in Application layer and hence he told me that am not suitable for the required position. They wanted the people who worked on hard core OS development or Device driver/firmware developers. Also he asked me that i know JAVA. I told, i have not worked on JAVA but i have studied in academic. After he told "there is no meaning in continuing this interview process. Even though i passed this round, other people will ask hard core JAVA questions. You are good as a application programmer but we require efficient C programmers and JAVA programmers. Efficiency in Application domain is not enough for OS and below layers. I am sorry to say, u can go now...."
I was so sad and scolded the person who scheduled my interview, came back.
After some days, i was trying to write the same program in one shot. But recently, with one loop i have done....!!!
/****************************************
* 2nd Version of the program
*
******************************************/
void main()
{
char str[]="I am Pramod";
int i=strlen(str);
while(i!=0)
{
if(str[i]!=' ')
{
i--;
continue;
}
else
{
printf("%s\n",str+i);
str[i] = '\0';
i--;
}
}
printf("%s\n",str+i);
}
After this i have started to collect my friend's, colleague's ideas on this questions. After getting some ideas following program got its birth...!
/****************************************
* 3rd Version of the program
*
******************************************/
#define SWAP(T, a, b) { T t = (a); (a) = (b); (b) = t; }
char *strrev(char *s)
{
int len = strlen(s);
if (len > 1)
{
char *a = s;
char *b = s + len - 1;
for (; a < b; ++a, --b)
SWAP(char, *a, *b);
}
return s;
}
void main()
{
char *ptr;
char str[]="I Am Pramod";
ptr = strtok(strrev(str), ", \n");
while(ptr!=NULL)
{
printf("%s\n",strrev(ptr));
ptr = strtok(NULL,", \n");
}
}
Every tough questions will improve our thorughtprocess about the problem or programming languages. Our approach skills will be improved. Hence it is always recommended to attend interviews NOT ONLY to change the job . At least mock interviews with domain experts/architects will help us a lot.
If anyone of you find different logic, please drop a mail onto pramoda.ma@gmail.com
Good luck...