Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

void print(int num){
if(num<10){
printf("%d",num); return;
}
// do something with it
print(num/10);
printf("d%", num%10);
}

would this work?

- Anonymous October 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. It would. You'll just need a check for negative numbers.

public void print(int number) {
		if(number < 0) {
			System.out.println('-');
			number *= -1;
		}
		if(number < 10) {
			System.out.println(number);
		} else {
			print(number / 10);
			System.out.println(number % 10);
		}
	}

- smffap November 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes. It would. You'll just need a check for negative numbers.

public void print(int number) {
		if(number < 0) {
			System.out.println('-');
			number *= -1;
		}
		if(number < 10) {
			System.out.println(number);
		} else {
			print(number / 10);
			System.out.println(number % 10);
		}
	}

- smffap November 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@smffap - what abt numbers like 10 or 100?

Here's a compiled/executed (non-recursive) solution that should handle all special cases

public void PrintDigits(int num)
            {
            int count = 0;
            int digit = 0;
            if (num < 0)
            {
                Console.WriteLine(" - ");
                num *= -1;
            }

            if (num < 10)
            {
                Console.WriteLine("Digit is {0}", num);
                return;
            }

            while (num >= 10)
            {
                digit = FindDigit(num, out count);
                Console.WriteLine("Digit is {0}", digit);

                int temp = (int) Math.Pow(10, count);
                temp = digit * temp;
                num = num - temp;
                
                //For numbers like 100, 1000
                if (num == 0 && count > 1)
                {
                    for (int i = 0; i < count; i++)
                    {
                        Console.WriteLine("Digit is {0}", num);
                    }
                    return;
                }


                //For last digit found
                if (num < 10)
                {
                    Console.WriteLine("Digit is {0}", num);
                    return;
                }

                count = 0;
            }
            return;
        }

        private int FindDigit(int number, out int countTens)
        {
            countTens = 0;
            while(number >= 10)
            {
                number /= 10;
                countTens++;
            }
            return number;
        }
        
    }
}

- aptsensdet July 06, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void printDigitsInLine(int n)
{

int power= (int)Math.log10(n);
int _base= (int)Math.pow(10, power);
if(n==0)
System.out.println(n);
while(_base >0)
{
int p =n/_base;
System.out.println(p);
n= n%_base;
_base= _base/10;
}
}

- Nel October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are using a temporary variable n here

- Aj October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Aj,
It can easily be turned into without the _base and power variables
public static void printInLine(int n)
{
if( n <0)
while(Math.abs(n) >10)
{
System.out.println(n/((int)Math.pow(10, (int)Math.log10(Math.abs(n)))));
n= Math.abs( n%((int)Math.pow(10, (int)Math.log10(Math.abs(n)))) );
}
else
while(n >10)
{
System.out.println(n/((int)Math.pow(10, (int)Math.log10(n))));
n= n%((int)Math.pow(10, (int)Math.log10(n)));
}
System.out.println(n);
}

- Nel October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Don't use Math.pow. It's likely to be slow because it's intended for doubles, and might possibly have weird associated roundoff errors (not saying it necessarily will in this case). I would recommend doing a loop to see what the greatest power of 10 less than n is, assigning it to a variable m, and then doing print (n / m) % 10; m = m / 10; in a loop until m drops to 0

- eugene.yarovoi October 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

By the way, using an extra on-stack variable is not using "extra space" because it wouldn't be possible to solve the problem if it were. When you do something like
System.out.println(n/((int)Math.pow(10, (int)Math.log10(Math.abs(n)))));
n= Math.abs( n%((int)Math.pow(10, (int)Math.log10(Math.abs(n)))) );

some of those intermediate values could be saved to the stack anyway.

- eugene.yarovoi October 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void print(int);
int main()
{
int i;
scanf("%d",&i);
print(i);
return (0);
}

void print(int digit)
{
if(digit/10==0)
printf("%d\n",digit);
else
{
print(digit/10);
printf("%d\n",digit%10);

}
}

- jyoti October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The order is reveresed

- Chen.Yangho October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

HI jyoti. I do not this this will not print line by line. if we provide the input number is 1234. As per the question, it supposed to print 4 lines. Your code will print only last digit.

How is this? This is C# code

for(int i=(int)(Math.Log10(inValue)/Math.Log10(10) + 1);i>0;i--)
{
Console.WriteLine((int)(inValue/Math.Pow(10,i-1)));
inValue = inValue % (Int)Math.Pow(10, i - 1);
}

- N.M October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have executed this program and it works well as per requirement.

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I have executed this program and it works well as per requirement. Because its a recursive call so it prints all numbers line by line.

- jyoti October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It does not work. It is returning the output as below for the input 1234

1
12
123
1234

expected output will be
1
2
3
4

- N.M October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include <iostream>
#include <cmath>
using namespace std;
void Funct(int);
int main()
{
int n;
cout<<"Enter a no: ";
cin>>n;
Funct(n);
return 0;
}
void Funct(int temp)
{
int i;
cout<<"The OutPut is: "<<endl;
for(i=pow(10,int(log10(temp)));i>0;)
{
cout<<temp/i<<endl;
temp=temp%i;
i=pow(10,int(log10(temp)));
}
}


Try this one out...
It is producing the desired result...

- Shantanu Konwar October 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

What type of silly question AZ-US asks for? This is just CS101 programming course while teach how to write simplest recursive program!

- anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

<pre lang="" line="1" title="CodeMonkey92430" class="run-this">import java.io.*;
import java.text.*;
public class Digits
{
public void printDigit(int n)
{
if (n>=10)
printDigit(n/10);
System.out.println(n%10);
}

public static void main(String[] args)
{
int num = 0;
System.out.println("Enter any number");
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = in.readLine();
DecimalFormat df = new DecimalFormat();
Number n = df.parse(s);
num = n.intValue();

}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
System.out.println("number is: " + num);
Digits d = new Digits();
d.printDigit(num);

}
}</pre><pre title="CodeMonkey92430" input="yes">
</pre>

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Recursion is actually usage of extra space. :-)

- Anonymous October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Recursion is actually usage of extra space. :-)

- Anonymous October 13, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void printval(int n)
{
if(n==0)
return;
printval(n/10);
printf("%d\n",n%10);
return;
}

- geeks October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The print statement should be before the recursive call
void printval(int n)
{
if (n==0) return;
System.out.println(n%10);
printval(n/10);
return;
}

- Aj October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Aj, your solution prints in reverse order. ie 1234 will print 4321.
If you put the print statement after the prinval, it will print in the right order but it fails if n=0;

- Nel October 08, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

0 won't work, it should like this:
void printval(int n)
{
if(n>=0 && n<=9){
printf("%d\n",n);
return;
}
printval(n/10);
printf("%d\n",n%10);
}

- xoxo October 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is pretty simple

- N.M October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printDigits(int digit)
{
if (digit >= 10)
{
printDigits(digit / 10);
System.out.println(digit % 10);
}
else
{
System.out.println(digit);
}
}

- Shailesh October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursion uses extra memory on stack. So you should not use Recursion.

- Anonymous October 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

f(N){

if(N ==0)
return;
if(N > 0)
f(N/10);

print(N%10);


}

- random October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Of course , additional storage in implicit in recursion, and hence the above pseudo code involves that.

- random October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This looks good, I am using only two variables i.e. x=number, n=digit. I feel use of this variable should not be considered as temp. Correct me if I am wrong.


#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

void print(int x, int n){
while(n>0){
cout<<x/int(pow (10,n-1))<<"\n";
x=x%int(pow (10, n-1));
n--;
}
}

int main(){
int number=1054;
int digits=4;
print(number,digits);
return 0;
}

- hellboy October 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{private static void print(int n) {

if (n > 0) {
print(n / 10);
System.out.println(n % 10);
}
}}

- leo October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while(n>0) {
cout<<n%10;
n=n/10;
}
return;
Either I understood the question wrongly or u guys did :D

- Anonymous October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Dude you solution will print digits in reverse.
i.e. For input 1234 your sol will print
4
3
2
1

but the required out put is
1
2
3
4

- Anonymous October 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

See my solution below (using recursion):

int main()
{
  PrintNum(1024);
}

void PrintNum(int n)
{
  if (n == 0)
    return;
  PrintNum(n / 10);
  cout << n % 10 << endl;
}

- Masrur.Mahmood October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include "stdio.h"

print_d(int num)
{
if(num/10 == 0)
{
printf("%d\n",num%10);
return;
}
print_d(num/10);
printf("%d\n",num%10);
}
int main()
{
print_d(1024);
return 1;
}

- pago October 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
void s(int);
main()
{
int n;
printf("Enter a number\n");
scanf("%d",&n);
s(n);
}

void s(int n)
{
if(n>=10)
s(n/10);

printf("%d\n",n%10);

}

- Puran chand October 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Integer temp = 532323;
String s = temp.toString();

for(int i = 0; i<s.length(); i++){
System.out.println(s.charAt(i));
}

- Anil October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol.. ;)

- kn January 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>



void print(int n)
{
    if(n <=0)
     return ;

     print(n/10);
     n =  n%10;
     printf("\n%d\n",n);
}


int main(void)
{
    int n = 1024;
    print(n);
    return 0;
}

- PeerPanditBadshah October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey13867" class="run-this">class Main
{
public static void main (String[] args) throws java.lang.Exception
{
PrintNum(1024);
}

public PrintNum(int n)
{
while (n != 0)
{
System.out.println(n % 10);
n = n / 10;
}
}
}

</pre><pre title="CodeMonkey13867" input="yes">1024
</pre>

- Masrur.Mahmood October 27, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm sorry, the above one is wrong. I did the answer in reverse order. Here's the correct one:

class Main
{
  public static void main (String[] args) throws java.lang.Exception
  {
     PrintNum(1024);
  }

  public void PrintNum(int n)
  {
    if (n == 0)
      return;
    PrintNum(n / 10);
    System.out.println(n % 10);
  }
}

- Masrur.Mahmood October 27, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

void printrecursive(int num)
{
if(num >= 1)
printrecursive(num/10);

if(num != 0)
printf("%d\n", num%10);
}

int main()
{
printrecursive(3465);
return 0;
}

- trimmer November 17, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey55407" class="run-this">class PrintNumber {

public void print(int number) {
if(number < 0) {
System.out.println('-');
number *= -1;
}
if(number < 10) {
System.out.println(number);
} else {
int pow = (int) Math.pow(10, (int) Math.log10(number));
do {
System.out.println(number / pow);
number %= pow;
pow /= 10;
} while(pow != 0);
}
}

public static void main(String[] args) {
PrintNumber instance = new PrintNumber();
instance.print(0);
instance.print(1);
instance.print(10);
instance.print(12);
instance.print(122);
instance.print(-1);
instance.print(-123);
}
}
</pre><pre title="CodeMonkey55407" input="yes">
</pre>

- smffap November 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

No recursion needed. Just get the multiple of 10 to be used as divisor first and keep going.

int n = 45678;
	printf("%d\n", n);
	int m = 1;
	int p = n;
	while(n)
	{
		n /= 10;
		if (n) m *= 10;
	}
	n = p;
	do
	{
		printf("%d\n", n/m);
		n -= (n/m)*m;
		m /= 10;
	} while(n);

So m is 10,000 to start with. int division of 45678 with 10000 gives you 4. Now subtract 40000 from 45678. It gives us 5678. Divide m by 10 to get 1,000

int division of 5678 with 1000 gives 5 and so on.

Handles -ve, 0 and +ve. It keeps printing the -ve for each number. Handling it is left as an exercise to the reader.

- KK December 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
Integer num = 102345;
int i=0;
while(i<num.toString().length()){
System.out.println((int)(num/Math.pow(10,num.toString().length()-(i+1)))%10);
i++;
}
}

- kn January 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <math.h>

void fn(int val) {
  int n=log10(val);
  int i = n;
  for(;i>0;i++){
     printf("%d\n",val/pow(10,i));
  }
}

- redratio1 May 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Step 1: Find the number of digits in the number using log() fn, (let it be stored in a variable x).
Step 2: find number mod 10^(x+1) (store it in a variable y)
Step 3: find y divided by 10^x, print the result.
Step 4: decrement x by 1.
Step 5: go back to step 2 x>=0.

- Meraj Ahmed November 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count_digit(int n)
{
int count=0;

while(n)
{
count++;
n/=10;
}
}


void print_digits(int n)
{
int d=count_digit(n);
int count=d;
while(count>0)
{
n=(n/10) + (n%10) * (int)( pow(10,d-1) );

count--;
}


while(n)
{
cout<<(n%10)<<endl;

n/=10;
}
}

- Anonymous October 10, 2014 | 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