Intel Interview Question for Software Analysts


Country: United States




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

This is a class definition and class definition data is stored on heap. Whenever class is instantiated, object(i.e. member variables encapsulated as object) are stored in the heap.

Stack comes into picture only when function gets called. If function has primitives data type , those are stored in stack. If function has reference to objects , those objects will be created on the heap but reference variables to those objects will be on stack. When function exits , all reference variables stop pointing to heap, thus making the object created during function call eligible for garbage collection.

Answer to your question is heap.

- Anonymous February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I dont think memory will be allocated by JIT till those variables are used. In the code those are not even used.

- hint February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We were just shown the declaration of the class. There may well be code that uses the class that's not shown.

- eugene.yarovoi February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

agree Eugene , but we can not assume, if this was the code shown the interviewer then my answer will be no memory is allocated , depending on more code answer will change - heap or stack. I am trying to find right answer interviewer is expecting based on information given in the question ... evaluating out the knowledge of memory allocation (ref type or value type , locale/global etc ) in .net requires more code to be shown.

- hint February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We know it's a reference type due to the use of "class" rather than "struct".

I see your point though. You're thinking about the question of what's allocated when just this code runs. I was thinking about the question of what's allocated when this code is used to instantiate objects.

- eugene.yarovoi February 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

EDIT: ignore this answer. I misread the question as asking about C++. Can't delete the post for some reason.

Where the memory for a class is allocated depends on how you allocate it. The memory for the variables of a class is generally placed on the stack if you create the instance with notation like

Test myInstance;

The memory will be placed on the heap if you use the "new" keyword, like:

Test* myInstance = new Test();

Some classes may have internals that do dynamic memory allocation. In that case, even if the object is created using the first notation above and its variables are placed on the stack, the variables may point to memory that is dynamically allocated and hence on the heap. For example, consider a "vector" class whose variables might be:

int size;
int capacity;
T* backingArray;

While all 3 of the above variables will be stored on the stack if the vector is created on the stack, backingArray will still point to a location on the heap where the dynamically resized array begins. backingArray itself (the pointer) will be on the stack.

- eugene.yarovoi February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Intel Interview Question for Software Analysts
intel-interview-questions0
of 0 votes
7
Answers
C# Question.

class Test


int x;
int y;
Where the memory would be allocated ? Since both are primitive datatypes would it be allocated on stack or heap ?

- Stupid Developer 2 years ago in United States | Report Duplicate | Flag
Intel Software Analyst C#

Email me when people comment.

Country: United States


Email me when people comment.


5
of 5 vote
This is a class definition and class definition data is stored on heap. Whenever class is instantiated, object(i.e. member variables encapsulated as object) are stored in the heap.

Stack comes into picture only when function gets called. If function has primitives data type , those are stored in stack. If function has reference to objects , those objects will be created on the heap but reference variables to those objects will be on stack. When function exits , all reference variables stop pointing to heap, thus making the object created during function call eligible for garbage collection.

Answer to your question is heap.

- Anonymous 2 years ago | Flag Reply
1
of 1 vote
I dont think memory will be allocated by JIT till those variables are used. In the code those are not even used.

- hint 2 years ago | Flag Reply
0
of 0 votes
We were just shown the declaration of the class. There may well be code that uses the class that's not shown.

- eugene.yarovoi 2 years ago | Flag
0
of 0 votes
agree Eugene , but we can not assume, if this was the code shown the interviewer then my answer will be no memory is allocated , depending on more code answer will change - heap or stack. I am trying to find right answer interviewer is expecting based on information given in the question ... evaluating out the knowledge of memory allocation (ref type or value type , locale/global etc ) in .net requires more code to be shown.

- hint 2 years ago | Flag
0
of 0 votes
We know it's a reference type due to the use of "class" rather than "struct".

I see your point though. You're thinking about the question of what's allocated when just this code runs. I was thinking about the question of what's allocated when this code is used to instantiate objects.

- eugene.yarovoi 2 years ago | Flag
0
of 0 vote
EDIT: ignore this answer. I misread the question as asking about C++. Can't delete the post for some reason.

Where the memory for a class is allocated depends on how you allocate it. The memory for the variables of a class is generally placed on the stack if you create the instance with notation like


Test myInstance;
The memory will be placed on the heap if you use the "new" keyword, like:


Test* myInstance = new Test();
Some classes may have internals that do dynamic memory allocation. In that case, even if the object is created using the first notation above and its variables are placed on the stack, the variables may point to memory that is dynamically allocated and hence on the heap. For example, consider a "vector" class whose variables might be:


int size;
int capacity;
T* backingArray;
While all 3 of the above variables will be stored on the stack if the vector is created on the stack, backingArray will still point to a location on the heap where the dynamically resized array begins. backingArray itself (the pointer) will be on the stack.

- eugene.yarovoi 2 years ago | Flag Reply
Comment hidden because of low score. Click to expand.


Add a Comment
Name:


Writing Code? Surround your code with

and

to preserve whitespace.

- Anonymous March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ll still point to a location on the heap where the dynamically resized array begins. backingArray itself (the pointer) will be on the stack.

- eugene.yarovoi 2 years ago | Flag Reply

- Anonymous March 19, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.


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