Amazon Interview Question for Interns


Country: India
Interview Type: Written Test




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

#include<stdio.h>
#include<string.h>
void print(int n,int flag)
{
if(n<100)
{
printf("%d",n);
return;
}
int a=100;
int b=0;
if(flag==1)
{
a=a*10;
flag = 0;
b=1;

}
print(n/a,flag);

if(n%a==0)
{
printf(",%d%d",0,0);
}
else if(n%a<10)
{
printf(",%d%d",0,n%a);
}
else
{
printf(",%d",n%a);
}
if(b==1)
{
if(n%a==0)
printf(",%d%d%d",0,0,0);
else if(n%a<10)
printf(",%d%d%d",0,0,n%a);
else if(n%a<100)
printf(",%d%d",0,n%a);
else
printf(",%d",n%a);

}


}
main()
{
int a;
scanf("%d",&a);
print(a,1);
return 0;


}
if any thing wrong please comment here

- gupta.shankar91 January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void divide(int);
main()
{ long int n;
printf("Enter the number:");
scanf("%ld",&n);
divide(n);
}
void divide(int n)
{
char digit[30];
int d,i=0,count=0,j;
while(n!=0)
{
d=n%10;
n=n/10;
digit[i++]=d;
count++;
if(count==3 && i==3)
{
digit[i++]=',';
count=0;
}
else if(count==2 && n!=0 && i>3)
{
count=0;
digit[i++]=',';
}
}
i--;
j=i;
while(j>=0)
{
if(digit[j]==',')
printf("%c",digit[j]);
else
printf("%d",digit[j]);
j--;
}
}

- Arp January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

similar implementation in Java

public class ConvertFormat {
    public static void main(String[] args) {

        int number = 11200234;


        int rem1 = number % 1000;

        int length = 0;

        int temp = number / 1000;
        number = temp;

        while (temp != 0) {
            length++;
            temp = temp / 10;
        }

        if (length % 2 == 1) {
            System.out.print((int)(number / Math.pow(10, length-1)));
            System.out.print(",");
            length--;
            number = (int) (number % Math.pow(10, length));
        }

        for (int i = length; i > 0; i--) {

           if(i%2==0 && i!=length)
                System.out.print(",")  ;

            System.out.print((int)(number/Math.pow(10, i-1)));

            number=number%(int)Math.pow(10, i-1);
        }
        System.out.print(",");
        System.out.print((int)rem1);
    }
}

- megha January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just try the below one once.

//

void printRuppee(int count, int amount[])
{
	if(count < 3 && count > 0)
	{
		for(;count>0;)
			printf("%d", amount[(--count)]);
	}
	else
	{
		if(count%2 == 0)
		{
			printf("%d,",amount[(--count)]);
			printRuppee(count, amount);
		}
		else
		{
			printf("%d",amount[(--count)]);
			printRuppee(count, amount);
		}
	}
	return;
}
int _tmain(int argc, _TCHAR* argv[])
{
	int i = 1234567890;
	int m[100], r, c=0, t=0;

	for(;i!=0;)
	{
		m[c] = i%10;
		i = i/10;
		printf("%d %d\n",m[c],i);
		c++;
	}
	printf("\n");
	printRuppee(c, m);

	printf("\n");
	return 0;
}
//

- raghu January 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <stack>
using namespace std;
int main()
{
int n,i=0;
stack<int> s;
cin>>n;

while(n!=0)
{
s.push(n%10);
n=n/10;
i++;
}
while(!s.empty())
{
if(i%2==0 && i > 3)
{
cout<<s.top();
s.pop();
cout<<",";
i--;
}
while(!s.empty() && i!=3)
{
cout<<s.top();
s.pop();
cout<<s.top();
s.pop();
cout<<",";
i=i-2;
}

while(!s.empty())
{
cout<<s.top();
s.pop();
}
}
return 0;
}

- i hav used stack instead of array,most optimal solution June 08, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

here's the code for you man,
cheers!!!

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>

int main()
{
	long int num,c,x,i;
	printf("enter the fuckin number, PUNK!!!");
	scanf("%d",&num);
	long int b=num%1000;
	int a=num/1000;
	int n1=a;
	c=0;
	while(n1!=0)
	{
		c++;
		n1=n1/10;
	}
	if(c%2==1)x=c-1;
	else x=c;
	
	for(i=x;i>=0;i=i-2)
	{
		if(((a/(int)pow(10,i))%100)!=0)
		printf("%d,",((a/(int)pow(10,i))%100));
	}
	printf("%d",b);
	getch();
}

- gdg January 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

enter the fuckin number, PUNK!!!11111111111111
3,7,16,359
Your code gives this as putput for the above input

- Amy January 15, 2014 | 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