Google Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
18
of 18 vote

Physically stack and heap both are allocated on RAM and their implementation varies from language, compiler and run time

Stack is used for local variables of functions and to track function calling sequences. Heap is used for allocating dynamically created variables using malloc, calloc or new.
Stack memory is freed whenever the function completes execution but the heap memory needs to be freed explicitly using delete, free or by garbage collector of the language.

Stack memory of a process is fixed size and heap is variable memory.

Stack is faster than heap as allocating memory on stack is simpler just moving stack pointer up.

In case of multi threading, each thread of process will have a different stack but all threads share single heap

- Vijay March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
Can any one explain how garbage collector work to release the heap memory?

- Anonymous March 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Use reference counting.

- Noobie March 30, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Great answer. Also accessing memory on stack seems to be faster because of high memory locality.

- waiging.lau March 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Garbage collector works in two passes.
1. The system goes through the memory area and marks all the pointers which are pointing to the allocated memory.
2. Then it runs through all the unmarked memory area and declare them as free.

- Anonymous April 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Garbage collector looks for orphan leaves and deletes them

- spider April 10, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Basically Heap and stack both are memory partition done on RAM to store the variables value and methods respectively.

- ishwarchincholkar March 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Stack size can be changed too using alloca() system call, but it is discouraged to use it since it can cause stack overflow problem but overwriting some program's data.

Also there can be holes in Heap but no holes in Stack!

- Cleonjoys April 24, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More