The Stack vs. Heap Interview Question

April 19th, 2010

I heard an interesting interview question at Microsoft this week. The hiring manager asked the candidate, “What is the difference between the stack and the heap?” Like many interview questions, this particular question is either ambiguous on purpose (so the hiring manager can see if the candidate will qualify exactly what the question means) or ambiguous because the hiring manager has a particular scenario in mind. If I were answering the question, I’d begin by asking the manager if he means the difference between the stack and the heap in a C++ environment, in a .NET environment, or in general. Making sure you understand exactly what you are being asked in an interview is critical — it not only safeguards you from answering, perhaps perfectly correctly, a question that is not in your interviewer’s head, it also shows your interviewer that you are an active questioner. Anyway, in general the stack is memory which is usually allocated automatically by the operating system, and the heap is memory which is usually allocated and deallocated by the programmer. In a traditional C++ environment, the stack is used by the OS and compiler for things like managing function local variables and return values, and the heap is used for managing things like program-defined linked lists. In a .NET environment things are a bit trickier because .NET uses garbage collection (GC) and you generally don’t explicitly use pointers, but basically the stack still holds automatically managed data and the heap holds programmer-defined data.

Entry Filed under: Interviewing


Calendar

September 2010
S M T W T F S
« Aug    
 1234
567891011
12131415161718
19202122232425
2627282930  

Most Recent Posts