Google Interview Question for Software Engineer / Developers


Team: youtube
Country: United States
Interview Type: Phone Interview




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

Just make the program read characters from stdin.
exit only if a random sequence is entered.
will not hog cpu. will take a long long time to terminate :)

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

how do you check if a sequence is "random" ? checking the "randomness" of a sequence is similar to finding its kolmogorov complexity, which is not computable.

- ramgorur June 29, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

From "man alarm" -> " sleep(3) may be implemented using SIGALRM;"

#include <stdio.h>
 #include <signal.h>
 #include <limits.h>
 #include <unistd.h>
 
 void sighandler(int sig){
     printf("Don't be alarmed!! \n");
 }
 
 void
 sleep_alarm(unsigned int secs)
 {   
     sigset_t new, susp;
     
     signal(SIGALRM, sighandler);
     
     sigemptyset(&new);
     sigaddset(&new, SIGALRM);
     sigprocmask(SIG_BLOCK, &new, &susp);
     
     alarm(secs);
     
     sigdelset(&susp, SIGALRM);
     sigsuspend(&susp);        
 }   
 
 int main(int argc, char *argv[]){
     int i;
     for (i = 0; i <= ULLONG_MAX; i++){
         sleep_alarm (100);
     }

 }

- Mia February 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

Set thread priority to lowest possible value (still settable in user space) and have a while loop that increments a large integer and exits when integer gets big enough. This doesn't hog the CPU from other running processes and never blocks. It's depedent on OS scheduling and system load, though.

- Anonymous January 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

Write a code to compute a very slow growing function like the Ackerman inverse. Since this function grows slowly it will take a long time before the variable's range is over when this is over we start over again. If we do this in nested loops then we could write a very long running program.

- Anonymous January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

THis will hog CPU.

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

use all available mem on the machine to store a very very large integer.
Then, starting from 0, increment this integer by 1 in each loop iteration until
we get 0xfffffff ... ffffffff at the end

as an example, if you use 4 words to represent an int,
incrementing by one can be realized as follows:

int w[4];
w[0] += 1;
w[1] += (w[0] == 0);
w[2] += (w[1] == 0);
w[3] += (w[2] == 0);

- 111 January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Using all available memory hogs system resources. Constantly looping and incrementing an integer hogs CPU.

- Anonymous January 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

open a file write somthing very very long :) to it. close the file and read it. Once file is completely rad, terminate the program.

- Tinu February 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I will do this via a huge ASM code that involves only NOP instruction, which consumes space, is executed but does nothing.

- Swapnil February 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can make the parent process wait for child pid and start child suspended. After forking we can do sigalarm for parent and SIGCONT the child once we elapse the 'longtime'

// sigalarm proc
	static int i;
	i++;
	if( i < longtime) {
		alarm(5);
	} else { // finish by waking up child
		kill(pid, SIGCONT);
	}

        // main
        pid = fork();

        // child process
	kill(getpid(), SIGSTOP);
	return 0;
       

       // parent process
       // start timer
       signal(SIGALRM, send_event);
	alarm(5);
       // wait for child; this will complete 
       // only when child is continued
	waitpid(childpid, &status, 0);

- enomem February 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

pthread_yield may prevent to hog cpu...?

#include <pthread.h>
#include <time.h>

int main() {
    time_t t = time(NULL);
    while(time(NULL) - t < 0xFFFFFFFF){
		pthread_yield();
	}
    return 0;
}

- tmash06 February 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Does this do it?
I thought of a blocking read on a resource in my process (so no system resources) with a timeout. Then I searched for some correct code... could have only generated psuedo code from memory. Don't know if I could get away with that... :-( my Google interview is coming up :0! eek!
From:
stackoverflow.com/questions/3711830/set-a-timeout-for-reading-stdin

// timeout structure passed into select
    struct timeval tv;
    // fd_set passed into select
    fd_set fds;
    // Set up the timeout.  here we can wait for any number of hours.
    tv.tv_sec = 60*60*24;
    tv.tv_usec = 0;

    // Zero out the fd_set - make sure it's pristine
    FD_ZERO(&fds);
    // Set the FD that we want to read
    FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
    // select takes the last file descriptor value + 1 in the fdset to check,
    // the fdset for reads, writes, and errors.  We are only passing in reads.
    // the last parameter is the timeout.  select will return if an FD is ready or 
    // the timeout has occurred
    select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
    // return 0 if STDIN is not ready to be read.
    return FD_ISSET(STDIN_FILENO, &fds);
}

- Graham February 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think solution should be some program using IO (but not one like scanf) or network connection or DB connection.

e.g.
1) program that will follow this,
a) write 1 byte at a time to a file upto XYZ MB or kb
b) delete that file
c) repeat steps a, b N number of times, depending on how long you want your prog to run.

2) Keep Sending XX bytes of data to some random ip address (not internal)

3) Assuming you have some RDB connection,
create small table in database and keep inserting and deleting single record in that table.

- kasi.beck March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think solution should be some program using IO (but not one like scanf) or network connection or DB connection.

e.g.
1) program that will follow this,
a) write 1 byte at a time to a file upto XYZ MB or kb
b) delete that file
c) repeat steps a, b N number of times, depending on how long you want your prog to run.

2) Keep Sending XX bytes of data to some random ip address (not internal)

3) Assuming you have some RDB connection,
create small table in database and keep inserting and deleting single record in that table.

- kasi.beck March 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

my idea is to write a loop for printing data through a printer like..i=10000000000000000000000000000....000 then
while(i)
{
send data to printer to print;
i--;}

- nare July 07, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

writing Socket server: accept system call block for client to connect, program keeps running until the process terminates.

- anand.moon July 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Write a program to open a socket and calls "accept", it terminates whenever a client connects to it..

- confused_banda November 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this question will exmamine you knowledge of computer Arch and OS, here is some of my idea:
1) make your C program work as an IST for a never-happend Interrupt
2)make your C program work as an handler for a never-happend SWI entry
3)make your C program able to change his PCB to give up his CPU cycle

- Anonymous December 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

Well I guess this guy is in the funny mood to ask this kinda question. However, I have the humorous solution for him..

Why don't U give a code of any recursive or backtracking problem.

I would have given Tower of Hanoi with 512 disk.. Code will not take forever to finish yet it will have a great deal of time... haha

No hogging no wait no resource blocking

- hprem991 January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No hogging? That would hog.

- Memo January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How is this not hogging CPU? It would most likely be CPU-bound, meaning 100% CPU usage on the core on which it runs.

- Anonymous January 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

It is not possible to write a program that runs very long, does not sleep and does hog cpu (unless its the OS for mars curiosity rover). A paradox. Probably they were testing Operating system knowledge.

- asrivas2 February 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Keep on doing some I/O operations in a tight loop for a longer duration.
Is it enough ?
for(.......)
{
scanf("%d",&n);
scanf("%d",&n);
................
}

- Anonymous January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I might not be understanding the original question correctly, but scanf is a blocking call so that won't work - i.e. the program will not terminate. I think a better solution may be to use signals and threads, e.g. use an user defined signal that is triggered after a "very very very" long time that terminates a thread that is waiting or doing some low-CPU computation.

- Heyzeus January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Not sure but what about coding an operating system? It is in effect a really long program and it manages resource usage as well.

- ssn January 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 votes

Problem says it should not block resources

- Anonymous January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
1
of 1 vote

You don't understand the definition of NP, do you...

- Anonymous January 25, 2013 | Flag


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