Interview Question


Country: United States




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

Well, I am just gonna give it a try. Expect smart comments:

#include<stdio.h>
#include<time.h>//for another implementation..!
#include<signal.h>

#define wait_sec 1

void run_loop();
void myHandler_1(int );
void myHandler_1(int );
int main()
{
  run_loop();   
}
void run_loop()
{
   static int print_cal = 1;  //initialized to 1; as question is prompting..
   while(1)
     {
        signal(SIGINT,  myHandler_1);
        sleep(wait_sec);
        printf("%d", print_val);
        ++print_val;
     }
}
void myHandler_1(int sig)
{
   signal(sig, SIG_IGN);
   pause();
   signal(SIGTSTP, myHandler_2);
}
void myHandler_2(int sig)
{
   signal(sig, SIG_IGN);
   run_loop();
}

PS: This is very rough sketch(I have not even compiled the code). So there is LOT of scope for improvement.
Also, this is machine + OS dependent code.

Another implementation that i can think of is

/****
  now = clock();
  while(clock() - now < 1);

  printf("%d", print_val);
  print_val++;
***/

And yes, have a glace at includes again.. may be dos.h is needed..

- Anonymous July 01, 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