Amazon Interview Question for SDE1s


Country: United States
Interview Type: Written Test




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

doors with numbers 1, 4, 9 ...
Basically door numbers which is a perfect square is the answer.

- Vin June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 votes

Good one(+1 for you) and reason I see is that perfect square has odd number of factors. Since the doors were all initially closed, an even number of factors for a given door number closes it and the odd number of factors opens it.

- Murali Mohan June 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 votes

yeah, and the reason is only those numbers will be opened which will have odd numbers of factors, because only factors of the numbers are responsible to toggle the door and it should be odd to be opened.

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

Elements with odd number of unique factors including 1 will remain open (only the Square numbers will satisfy that condition)

- pras July 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Code to demonstrate what Vic said:

public class Doors {
    public static void main(String[] args) {
        boolean[] doors = new boolean[101];
        for (int i = 0; i < doors.length; ++i) doors[i] = false;

        for (int iteration = 1; iteration <= 100; ++iteration)
            for (int i = 1; iteration * i < doors.length; ++i)
                doors[iteration * i] = !doors[iteration * i];

        for (int i = 1; i <= 100; i++)
            System.out.println("door " + i + " is " + (doors[i] ? "open" : "closed"));
    }
}

- cooldude June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

only 10 doors remain open perfect squares of 1,2,9,16,25,36,49,64,81,100 ..

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

done!

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <string.h>

#define OPEN 1
#define CLOSE 0

int main()
{
    int doors[100];
    int *d,i,m;
    d=&doors[0];
    for(i=0;i<99;i++)
        doors[i]=CLOSE;

    printf("Entered data");

    int inc=0,j=0;
    for(inc=1;inc<100;inc++)
    {
        m=100/inc;
        for(j=0;j<m;j++)
        {
            d=d+inc;
            if(*d==OPEN)
            {
                *d=CLOSE;
            }
            else{*d=OPEN;}
        }
        d=&doors[0];
    }

    for(i=0;i<100;i++)
    {
        if(doors[i]==OPEN)
        {
            printf("\t %d",i);
        }
    }
}

//enjoyy

- deeg June 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void door_open(int a[])
{
int i;

int a[100];
/* initialising array to 100 numbers */
for(i=1;i<=100;i++)
 a[i]=i;
/* here it will toggle 2,3,4..... till 100 */
	int cnt=2;
	while(cnt!=100)
	{
	for(i=cnt;i<100;i=i+cnt)
	a[i]=0;

	}
/*printing all doors opened */
	for(i=1;i<100;i++)
	printf("%d\t",a[i]);

}

- santhosh K June 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A simple bit manipulation is enough instead of maintaining arrays.

1. for n = total no. of gates, 
2. iterate over the bit set 
  2.1 for the ith iteration,
    2.1.1 iterate over the bit set and toggle every ith bit in the bit set ( provided i < n && i < current bit position).

- Krishna June 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we start toggling from first element onward, then answer would be
1,4,9,........(if index of the array start from the 0)...

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

1 4 9 16 25 36 49 64 81 100

#include <stdio.h>


int main () {

  int door[100];
  int i = 0; //0 - close, 1 - Open
  int j;
  for (i=1; i <= 100; i++) {
     door[i] = 1;
  }
  j = 2;
  do {
    for (i=j; i <= 100; i ++ ) {
      if (i%j == 0) {
         door[i] = ~door[i];
      }
    }
   j ++;
  } while (j <= 100);


  for (i = 1; i <= 100; i ++ ) {
    if (door[i] == 1) {
       printf("%d ", i);
    }
  }
}

- vsagar June 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printDoorsOpenedOrClosed(int passes)
	{
		HashMap<Integer, Boolean> doors=new HashMap<Integer, Boolean>();
		int i = 0;
		//Open all doors as default in the start
		while(i < passes)
		{
			doors.put(i, false);
			i++;
		}

		for(int j = 1; j <= passes; j++)
		{
			for(int k = j - 1; k < passes; k += j)
				doors.put(k, !(doors.get(k)));
		}
		
		for(int l = 0; l < passes; l++)
		{
			System.out.println("The door number "+(l + 1)+" is "+(doors.get(l) == true ? "Opened" : "Closed"));
		}
	}

- Anirudh August 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Only the 1st gate will remain open all others will be closed.

- Ashish Singh June 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

only 1st gate will remain open because the door toggle from 2nd to 100th

- Subodh Singh July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

only 1st gate will remain open because the door toggle from 2nd to 100th

- Subodh Singh July 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

close=1,open=0;
initial state is 1 1 1 1 1 1...
after first toggle
:-0 0 0 0 0.....
after second toggle(toggle every second element)
:-0 1 0 1 0....
after third toggle
:-0 1 1 0 0....
after fourth toggle
:-0 1 1 1 0...
after fifth toggle
:-0 1 1 1 1

if we do so the answer would be, one gate left after all(100) the iterations.

- Ake August 02, 2013 | 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