Interview Question for Developer Program Engineers


Country: India




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

Hi

Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:

call getrlimit to get current stack size
if current size < required stack size then
call setrlimit to increase stack size to required size
In C that might be coded something like this:

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 64L * 1024L * 1024L;   // min stack size = 64 Mb
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}

- rahul chaudhary March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will solve the issue temporarily.

I need a programming technique i.e we write our program in such a way that it deals with above described problem optimally.

increasing stack size is helpful, but to what limit can we increase? second thing is, we may come across above situations every time, so increasing stack may not be required.
so I need a dynamic way to deal with it..

- pnkjtr March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi

Normally you would set the stack size early on, e,g, at the start of main(), before calling any other functions. Typically the logic would be:

call getrlimit to get current stack size
if current size < required stack size then
call setrlimit to increase stack size to required size
In C that might be coded something like this:

#include <sys/resource.h>

int main (int argc, char **argv)
{
    const rlim_t kStackSize = 64L * 1024L * 1024L;   // min stack size = 64 Mb
    struct rlimit rl;
    int result;

    result = getrlimit(RLIMIT_STACK, &rl);
    if (result == 0)
    {
        if (rl.rlim_cur < kStackSize)
        {
            rl.rlim_cur = kStackSize;
            result = setrlimit(RLIMIT_STACK, &rl);
            if (result != 0)
            {
                fprintf(stderr, "setrlimit returned result = %d\n", result);
            }
        }
    }

    // ...

    return 0;
}

- rahul chaudhary March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Possible solution of mine : is to spawn a thread and call function in new created thread

- Tsarapin March 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I'm not sure whether you're asking how to increase the stack limit, how to emulate a function call stack using a loop and a stack datastructure, or how to debug a situation where a bug results in runaway recursion and a stack overflow.

- eugene.yarovoi March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
My query was to know a optimal programing technique to deal with the problem as described below.
like i have a program where, there is a multilevel of invocation of methods within from prev method.
like: I invoked method A(), which in turn calls method B() within from itself. Now B() will get executed and its activation frame will occupy space in stack. before B() gets completed , it calls C(), later C() calls another D().... etc etc....
so if this continues , at one point of time, we will exhaust the allocated stack space for our program(process).

if such situation arises, whats the best programming technique to deal with this ?
I am not talking about increasing the stack size.

- pnkjtr March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi,
My query was to know a optimal programing technique to deal with the problem as described below.
like i have a program where, there is a multilevel of invocation of methods within from prev method.
like: I invoked method A(), which in turn calls method B() within from itself. Now B() will get executed and its activation frame will occupy space in stack. before B() gets completed , it calls C(), later C() calls another D().... etc etc....
so if this continues , at one point of time, we will exhaust the allocated stack space for our program(process).

if such situation arises, whats the best programming technique to deal with this ?
I am not talking about increasing the stack size.

- pnkjtr March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I suppose the question would really be why your program's stack becomes so big. Are you seeing more stack frames than you're supposed to be seeing because of a bug in your code?

- eugene.yarovoi March 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

in my opinion it is *really hard* to overflow the stack (which is about 8mb in linux) unless you program has unterminated recursions or plays around with stack frame pointer..

though one quite natural suggestion would be not to pass the function parameters "by value" and try to minimize the number of formal parameters (e.g, using global variables) especially for recursive calls

- Anonymous March 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think you should have a try catch block in your program and if the stack overflows catch the error and can print the stack.

- Nipun March 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

replace the recursion logic with infinite loop logic with proper exit conditions. So it will not fill the stack but use the CPU time to compute whatever the depth of the input.

- suresh Vadivel March 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

For me common sense seems to be check the stack size early in function and return with error to avoid further stack usage and help in stack unwinding.

Algo:
call getrlimit to get current stack size
if current size < required stack size then return immediately with error

- sanjay.kr.maurya June 24, 2012 | 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