Amazon Interview Question for Software Engineer / Developers






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

According to me it can't be done. See http://www.research.att.com/~bs/bs_faq2.html#resume

I did not said any thing and as you might expect the interview was a complete disaster...
BEST OF LUCK TO U GUYS

- CyberPhoenix October 02, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A) You can catch this exception in the second function then either 1) rethrow so that the first function can catch it then deallocate appropriately 2) Catch and return an error value

B) The second function doesn't have to catch it at all. The exception would automatically be rethrow to the first function.

- ano October 03, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Guys i present a simple solution for C/C++ , it very easy in JAVA so i ma not writing for JAVA

#include <unistd.h> /* defines fork(), and pid_t. */
#include <sys/wait.h> /* defines the wait() system call. */
int status=0;
void main()
{
/* storage place for the pid of the child process, and its exit status. */
pid_t child_pid;
int child_status;
/* lets fork off a child process... */
child_pid = fork();
/* check what the fork() call actually did */
switch (child_pid)
{
case -1:/* fork() failed */
perror("fork"); /* print a system-defined error message */
exit(1);
case 0:/* fork() succeeded, we're inside the child process */
f1();
status=1;
exit(0);/* here the CHILD process exits, not the parent. */
default:/* fork() succeeded, we're inside the parent process */
wait(&child_status);/* wait till the child process exits */
if(0==status)//oops some exception occured during f1()
{
// do exception management, i mean free memory
}
}
}

- Anand Rai October 07, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Partial handling
Consider the following case:

void g()
{
throw "Exception";
}

void f() {
int* i = new int(0);
g();
delete i;
}
int main() {
f();
return 0;
}
Can you see the problem in this code ? If g throws an exception, the variable i is never deleted and we have a memory leak.

To prevent the memory leak, f() must catch the exception, and delete i. But f() can't handle the exception, it doesn't know how!

What is the solution then? f() shall catch the exception, and then rethrow it:

void g()
{
throw "Exception";
}

void f() {
int* i = new int(0)
try
{
g();
}
catch (...)
{
delete i;
throw; // This empty throw rethrows the exception we caught
// An empty throw can only exist in a catch block
}
delete i;
}
int main() {
f();
return 0;
}

- Jack of All October 08, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

why not use the try-finally pattern in the first function? in CPP it is directly not available but herb sutter blog uses try - catch (delete and throw)

- guna October 10, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There might be two solutions

1. Catch the exceptions in the function First and release the allocated memory however in practice recommended coding style is
Use auto_ptr or smart_ptr for the dynamic memory. Auto_ptr will release the memory in case of stack unwinding even in case of exceptions :)

- MG June 20, 2010 | 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