c Interview Questions | Glassdoor

c Interview Questions

273

interview questions shared by candidates

c Interview Questions

Sort: Relevance Popular Date

Game Programmer at Zynga was asked...

Feb 8, 2012
 How do you find the max depth of a binary tree? 5 Answers I used recursion, passing the current depth to each child and returning the max value they returned plus one to the caller. how about bfs? breadth first search will be better than the recursion method for min-depth searching, but works same as recursive in the case of finding max depth problem Show More Responses public static int maxDepth(TreeNode root) { if (root == null) return 0; //base case return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } This entirely depends on the implementation. It can be calculated as the integer log base 2 of the highest index of the tree. This would prevent quite a few cache misses from traversing the tree each time, and would be a fairly quick calculation using bitwise operations. int val;// the value int result;// the result int tmp; result = (val > 0xFFFF ? 0 : 1) >= result; tmp= (val > 0xFF ? 0 : 1) >= tmp; result |= tmp; tmp= (val > 0xF ? 0 : 1) >= tmp; result |= tmp; tmp= (val > 0x3 ? 0 : 1) >= tmp; result |= tmp; result |= (val >> 1); While this may seem like a lot, assuming the tree is small, these calculations are actually quite fast, don't involve branching, and don't require arbitrary pointers to sub-tree members. This works best for trees that are either complete or nearly complete, however, since otherwise there will be a lot of wasted memory for indices that contain nothing.

Software Development Engineer In Test Consultant at HCL Technologies was asked...

Aug 29, 2009
 What is a class? 4 Answers Just because you have years of programming experience, doesn't mean you won’t be asked entry-level question. Prepare for this. This is a reasonable question for a senior person - you should be able to explain basics to a client, manager or junior person. So a answer could be: A class is a group of objects with shared properties. Using classes reduces time needed for new coding and simplifies changes. Take responsibility for your actions. For someone with that much experience, you can't answer this stupid question? I do interviews and hires and if you gave me that lame excuse, I would terminate the interview on the spot. Every resume I look at looks the same (same experience, job titles, job responsibility). You need to make the difference in the interview, this is what distinguish you from the other 100 candidates. Hate to say it but, with the economy the way it is, it is easier to be choosy on who I hire. Show More Responses This guy (interview candidate) is hilarious, those three questions are the most fundamental concept of OOP, and he consider OOP entry-level? LOL. What have you done at Microsoft? Non-programming related job? Go back to any college and take a CIS 200 level class. Experience at Microsoft is not a pass for interview questions.

May 14, 2011
 Write a C function to define strstr(char *haystack, char * needle) to return the first occurrence of needle in haystack. Code must compile and execute. 4 Answers I wrote the necessary C code and didn't struggle much on code development as this is a common question. Can't guarantee that my code would compile and run as I didn't get a scope to run the code using a compiler. Not sure how picky they are about this matter. I wrote this in 15 mins, after being out of touch with C for 2-3 years. It compiles. Just in case someone needs it: int strstr(char *haystack, char * needle) { int i=0,j=0; int here=0; while(1){ if(needle[i]==haystack[j]){ if(here==0) here=j; i++;j++; if(i>=strlen(needle)){ return here; } if(j>=strlen(haystack)){ return -1; } } else{ j++;i=0;here=0; } } } in Java: static int indexOf(String main, String search){ int index = 0; for(int i = 0; i <= (main.length()-search.length()); i++){ if(main.substring(i, i+search.length()).equals(search)){ index = i+1; break; } } return index; } Show More Responses Wrong answer... int strstr(String haystack, String needle) { if (haystack == null || needle == null || haystack.equals("") || needle.equals("") || needle.length() > haystack.length()) return -1; int position = -1; int j = 0; for (int i = 0; i 0){ i--; j = 0; position = -1; } } return position; }

Mar 22, 2010

Software Engineer at Amazon was asked...

Mar 23, 2011
 Write a function that allows to convert a string to the corresponding number (i.e. implement the atoi() C function) 4 Answers int my_atoi(char* pStr) { if (pStr == NULL) { printf("ERROR: null string.\n"); return -1; } int num = 0; int pos; for (pos = 0; ; pos++) { char currChar = *(pStr+pos); // Check whether the current char is not a digit if ( currChar '9' ) return num; // Read the number and add it to the 'num' variable num = (num*10) + (int) currChar - (int)'0'; } return num; } int atoi(char *s) { int i = 0; while(*s) { i = (i<<3) + (i<<1) + (*s - '0'); s++; } return i; } int atoi(char *s) { int i=0; while(*s) { i = i*10 + (*s- '0'); s++; } return i; } Show More Responses Try testing for the above to sample codes, I have not really tested it on any compilers.

Financial Applications Engineer at Bloomberg L.P. was asked...

Jul 1, 2009
 (Programming Language Test) When casting an object of a polymorphic class from a base class, which kind of cast executes only if it's is valid? 3 Answers choices were a number of different kinds of casts - dynamic, static, reinterpret, etc. You should use dynamic cast. It returns a null pointer if it's not valid. Reinterpret will give you a bad pointer since the cast will be pointing to the wrong part of the polymorphic object. Also, it's important to note the difference between using pointers vs references in the case of casts. In C++, if you use a pointer in a dynamic_cast() and it fails, the result is a null pointer. However, since you cannot have a null reference, dynamic casts need to be wrapped in try/catch blocks.

Jun 27, 2011
 Given a set of Sentences containing lower case letters only, remove common phrases from each sentence. Here a phrase is defined by 3 or more consecutive words. E.g: S1 = "I my bye good"; S2 = "my bye good boy"; common phrase is: "my bye good" so the two sentence become S1 = "I"; s2 ="boy"; 3 Answers The expected answer from the interview is to find all common phrases. Now, since these phrases must be found in the first sentence, simply mapping word index of beginning of phrase in first sentence would do. The trick that I didn't realize is, though it says 3 or more, we actually need to consider 3 length ones only as a 4 length one will implicitly contain two 3 word phrases and the phrases can overlap. As for the part about finding common phrases, he expected hashing and I proposed that we can map each word as integer and apply the procedure on the integer equivalent. Though this mapping is similar to Hashing, but he wasn't accepting this answer and it got me confused on thinking, how could this be optimized further. if you write in python, dict is implemented in hash table; not like STL in c++, which implement map in RB-Tree. I found the right algorithm, but write code in c++ with map in STL, the interviewer failed me. def foo(s1,s2): return set(s1.split()).symmetric_difference(set(s2.split()))

Engineering at Fortify Software was asked...

Sep 30, 2011
 Find the vulnerabilities in this c code and exploit it: #include int main() { int leet=0; char buf[80]; puts("Hello, What's your name?"); gets(buf); printf("Hello, %s!\n", buf); if(leet = 31337) { FILE *fp; fp = fopen("grades/my-grades.txt", "w"); fprintf(fp, "Sweet! I got some points!\n"); close(fp); } return 0; } 3 Answers This is not the exact problem, but a very similar one that I found. Basically, there is a buffer overflow vulnerability with gets() that you can exploit. comparison (==) instead of assignment (=) in condition checking. gets buffer overflow error, you can input a string which is longer than 80, it will overwrite leet value, it could be 31337, then you can write the grade file.

Software Engineer at Pure Storage was asked...

Nov 11, 2010
 Given two base classes and a class derived from them how might one layout class instance memory so that polymorphism will work correctly. Problem was simplified in that one could assume memory was a series of same-sized slots and methods and attributes took up one slot each. 3 Answers I thought I did rather well, as I figured out (with some help) all but one aspect of the problem. hello , could you expand a little on the question and what you cam up with ? No, I can't really elaborate. It has been almost 3 years, the solution was convoluted, and I think I have blocked it out, since I don't remember much. I believe it involved having place holders for methods and attributes in the base class that carried over to the derived class. But I'd suggest a good compiler book, some time with the C++ compiler, and hours to kill if you really want to know the full answer.

Software Engineer at Blizzard Entertainment was asked...

Dec 21, 2009
 Write an application that maps out a large set of data in as little time as possible. 3 Answers The right answer was a parser that adds things to a map, and evaluating the map as you went for duplicates. I actually did not get the answer. Can you explain yourself better? What is a large set of data? Nowadays, we have solutions such as MapReduce to handle data intensive problems. See: http://en.wikipedia.org/wiki/Data-intensive_computing. For not so large dataset, you can use a simple map (as HashMap in Java) to store each partition of information.
1120 of 273 Interview Questions