c Interview Questions | Glassdoor

# c Interview Questions

271

interview questions shared by candidates

## c Interview Questions

Sort: RelevancePopular Date

### ASIC Design Engineer at NVIDIA was asked...

Apr 19, 2012
 Complete the C function (body) that uses recursion to determine if the string is a palindrome4 Answersint isPalin(char *str){ int l = strlen(str); return isPalinHelper(str,0,l-1); } int isPalinHelper(char *str,int i,int j){ if(i= tail) return 1; return (palindrome(head+1,tail-1) && (*tail == *head)); } int check_palindrome (const char* str) { const char* tail = str + strlen(str) - 1; const char* head = str; return palindrome(head,tail); } int main(int argc, void* argv[] ) { if (check_palindrome (argv[1])) printf("true\n"); else printf("false\n"); }At my previous solution, please ignore the local variable int val at the palindrome functionShow More Responsesbool palindrome(char* str, int len) { if(len<=1) return true; if(len == 2) if(str[0] == str[1]) return true; return (str[0] == str[len-1]) && palindrome(str+1, len-2); }

### Embedded Software Engineer at Dolby was asked...

Apr 17, 2011
 Write a program to test endianess of storage.3 AnswersI described a method that placed a 1 byte word into a 2 byte address. If the word was matched in the upper MSB, it was Big endian, otherwise small endian.use union property to judge if it is big endian or little endian.#define LITTLE_ENDIAN 0 #define BIG_ENDIAN 1 int EndiannessTest() { int i = 1; char *p = (char *) &i; if (p[0] == 1) // Lowest address contains the least significant byte return LITTLE_ENDIAN; else return BIG_ENDIAN; }

### Software Engineer at Ooyala was asked...

Dec 16, 2010
 given a string,which is filled with either "(" or ")", for example "(())()())", write a function to check if the string is "proper". A "proper" string means if there is a "(" in the string, there should be a ")" which corresponds to the "(" in the string. For example, "))()(" 3 Answerstwo sub tasks. One is to check if the number of "(" and ")" in the entire string are equal. The other task is to check the number of "(" is always no less than the number of ")" when traversal the string from the head of string to the end.you can traverse through the string in a loop and increment a counter on occurrence of '(' and decrement on occurrence of ')' .Return false if counter becomes negative anytime in the loop.Just a single stack, pop for ')' and push for '('. if isEmpty() when you encounter ')' means string is not proper

### Software Engineer at IBM was asked...

Jun 24, 2012
 to developd to class software describe it3 Answersto make all software in android techno... because bascially android is open source wihich is based on linux kernal so it's not possible to make all softwere in android..no... because bascially android is open source wihich is based on linux kernal so it's not possible to make all softwere in android..

### Senior Hardware Engineer at Apple was asked...

Mar 5, 2012
 Not the most difficult question but I found this question interesting. Write a piece of code that allows you to detect if the machine you are running the program is little Endian or big Endian memory structure. 2 AnswersTo answer this question, you need to know about the union in C. 1) declare union { unsigned long ul; unsigned char tab[4] } un; 2) then: un.ul=0x12345678; for(i=0; i<4; i++) printf("%x ",un.tab[i]);Much easier than that: inline int isLE() { int v = 1; //stored in host order char *p = (char *)&v; //can access addresses &p[0], ..., &p[3] //LS byte stored at lower address in memory => LE return p[0]; } I'm sure there's a more efficient way, but that's the best I know.

### Engineer at Microsoft was asked...

Nov 10, 2009
 Given a square Grid of numbers, Considering all the numbers at the boundary as 1 layer and numbers just inside as another layer and so on how would you rotate each of the layers of the numbers by a given amount.3 AnswersIs it OK if my solution pegs the CPU for 5 minutes? What about introducing a security hole - good?3 sets of arrays where: set 1 (multi-dimensional) array is for the original grid; set 2 is a single-dimensional array corresponding to each "layer" of the set 1 array; and set 3 is a single-dimensional array for the newly ordered (or "rotated" values) to be stored. Store the elements of the 1st layer (from set 1 array) into the 1st set 2 array. Store the elements from the 1st set 2 array into the 1st set 3 array (offset by +1 - except for last element). Eg: 1st element in set 2 array equals 2nd element in set 3 array, 2nd element in set 2 array equals 3rd element in set 3 array, so on. Last element in set 2 array equals 1st element in set 3 array. Store the elements from the 1st set 2 array back into the set 1 array - this "layer" is now rotated. Repeat for all layers !The approach would be simlar to rotating a sequence of number in an array of lenght n by k. Just more cumbersome to write. The interviewer asked me that, since i was answering all other questions well. I am pretty sure he heard this somewhere and din't knew solution himself.

### Software Development Engineer In Test (SDET) III at Microsoft was asked...

Apr 7, 2009
 Suppose you have a C++ base class and several classes that inherit from it. What might happen if you do not write a virtual destructor for the base class? 2 AnswersIf you reference the derived classes by a pointer of the base class type - the destructor of the derived classes will not be called upon destruction performed using that pointer.Only the base class destructor would be called and so any pointers/ memory allocated in the derived classes will remain with out being removed.

### Software Engineer at NVIDIA was asked...

Mar 5, 2012
 Substring function2 AnswersThis function return 0 when the sub string was not found at the given string or unsigned integer number bigger then 0, which indicate the number of occupancy. int my_strstr(const char* str, const char* to_find){ static int num_of_occur = 0; while (*str) { const char* tmp_s = str; const char* tmp_f = to_find; while ( (*tmp_f == *tmp_s) && *tmp_f) { tmp_f++; tmp_s++; } if(!(*tmp_f)) num_of_occur++; str++; } return num_of_occur; }Brute force accepted