Interview Questions

How would you test a Stack? ....

Microsoft Interview Questions and Answers


(Continued from previous question...)

88. How would you test a Stack? ....

Question:
What is Stack ? How would you test a Stack? The size of stack is not provided. Can you define stack size at run time?


maybe an answer:


size command gives the size of an object file after compilation( not at runtime) in linux . But the question is :run time stack.I think we have to use any psuedo file system like "proc" in linux distros. There associated with process id (which we can get using getpid()) and at all if we have permission to access /proc/"pid" directory then we can almost get any information related to process memory.

usually stack address starts from 0xffffffff. So if we find address of last defined variable in a function and subtract it from address above , we can approximately get stack size.


maybe an answer2:


Change the stack structure by adding a an extra element for saving stack size, somthing like

struct stackelement
{
int stackLen;
blah blah
stackelement *next;
}

every time you push a element do the following

a) pop a element read the stack size
b) push the element
c) increase the stack size and embed in the new item to pushed

to get the stack size
a) pop a element and read stack size
b) push the element back to stack

complexity is O(1)
disadvantage relatively more memory than usual stack.

(Continued on next question...)

Other Interview Questions