Amazon Interview Question for Software Engineer in Tests






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

public static long[] GetTheArray(int num)
        {
            if (num == 0)
                return null;
            
            long[] result;

            if (num == 1)
            {
                result = new long[1];
                result[0] = 1;
                return result;
            }

            int multiplier = 2;
            result = new long[num / 2];
            result[0] = 1;

            for (int i = 1; i < num; i++)
            {
                int prev = (int)result[i - 1];
                int next = prev * multiplier;

                if (next <= num)
                {
                    result[i] = next;
                }
                else
                {
                    break;
                }
            }
            return result;
        }

-------------------------------------------------------------------------------------
Questions:
1. How do I reduce the result array size? I took an initial array size of num /2 since it will never exceed the num /2 size. But how do we optimize it more?
2. Is this function thread safe if multiple users access this function at the same time?

- rits August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. Take the size of the array, max as
max = (int)log2(n)+1;

- pur3 September 02, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static ArrayList<Integer> GetTheArray(int num)
{
if (num == 0)
return null;

ArrayList<Integer> result=new ArrayList<Integer>();

if (num == 1)
{
//result = new int[1];
result.add(1);
return result;
}

int multiplier = 2;
//result = new int[];
result.add(1);

for (int i = 1; i < num; i++)
{
int prev = result.get(i - 1);
int next = prev * multiplier;

if (next < num)
{
result.add(next);
}
else
{
break;
}
}
return result;
}

- Anonymous February 07, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static ArrayList<Integer> GetTheArray(int num)
{
if (num == 0)
return null;

ArrayList<Integer> result=new ArrayList<Integer>();

if (num == 1)
{
//result = new int[1];
result.add(1);
return result;
}

int multiplier = 2;
//result = new int[];
result.add(1);

for (int i = 1; i < num; i++)
{
int prev = result.get(i - 1);
int next = prev * multiplier;

if (next < num)
{
result.add(next);
}
else
{
break;
}
}
return result;
}

- Anonymous February 07, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

while( true ){
if( (i & ~( k-1 )) > 0){
System.out.println(" " + k);
}
if( k>i)
break;

k=k<<1;


}

where i is the input.

- Anonymous August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain your logic a little bit?

- ritzy August 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int k = 0;
while( 1 << k < num)
 print(1<<k);
 k++;

- S August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int power = Math.floor(log(num)/log(2));
long numbers[power+1];
for (int i=0; i<=power; i++){
   numbers[i] = Math.pow(2,i);
}

- Anonymous August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IEnumerable<int> Gen()
{
int n = 1;
yield return n;
while(true)
{
yield return (n *= 2);
}
}

void main()
{
int limit = 300;
var answer = Gen().Where(n => n < limit).ToArray();
}

- TIGERoX August 31, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printsquare(int num)
{
assert(num >= 0);
int shift = 0;
while(num)
{
printf("%d\n",1 << shift);
num = num >> 1;
shift++;
}

}

- leehom liang September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int num=1;


  while( num <= LIMIT )
   {
        print( num );
        num<<=1;
   }

- JD September 01, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static int[] container;
static void Main(string[] args)
{
int i = 2, k =0;
int num =300;
container = new int[30];
container[k++] = i;
while (true)
{
i = i << 1;
if (i < num)
{
container[k++] = i;
}
else
{
break;
}
}
for (int j = 0; j < k; j++)
{
Console.WriteLine(container[j]);
}
Console.ReadKey();

- Abhay September 03, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int i=300;
	int ii=1;

	while(i)
	{
		cout<<ii<<endl;
		ii=ii*2;
		i=i>>1;
	}

- louis.fernando September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int main()
{
int num = 256;
int res = 1;

while(res <= num)
{
printf("%d ", res);
res = res << 1;
}
}

- Mahesh September 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

basically u cant able to reurn the whole array in c
like int[]func(int,int) is invalid in c in java we can retun this so i think indirectly interviewerv want to check that knowledge of interviwee
otherwise the question is relly simple and not the level of amazon.com

- Anonymous July 07, 2011 | 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