Amazon Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
3
of 5 vote

public class NineDigitNumber {
    public static boolean findTheNum(long num, int[] digits, int index) {
        if (index == digits.length - 1) {
            num = num * 10 + digits[index];
            System.out.println(num);
            
            return true;
        }
        
        for (int i = index; i < digits.length; i = i + 2) {            
            swap(digits, index, i);
            long nextNum = num * 10 + digits[index];
            if (nextNum % (index + 1) == 0) {
                
                if (findTheNum(nextNum, digits, index + 1)) {
                    return true;
                }
            }
            swap(digits, index, i);
        }
        
        return false;
    }
    
    private static void swap(int[] digits, int i, int j) {
        int temp = digits[i];
        digits[i] = digits[j];
        digits[j] = temp;
    }
    
    public static void main(String[] args) {
        int[] digits = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        findTheNum(0, digits, 0);
    }
}

- Jim August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The number found is 381654729

- Jim August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you explain your logic please! I can't understand why it is i = i+2 in the loop!!

- Psycho August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

To Psycho:
An odd/even digit must appear in an odd/even place.

- Jim August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Understood. :)
Thanks. Nice one!

- Psycho August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How are you keeping track of whether a digit is used or not? Don't you need a hash map here?

- Ashish Kaila August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, I don't need a hash map. Try to understand the two calls to swap(). :)

- Jim August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not everyone can understand this piece of simple recursive code.

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

I cant understand the code...can u elaborate it..plsss...

- Vignesh August 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

"381654729"
This is the only digit which satisfy all the required conditions.

Process is simple. With the help of "Rules for Divisibility" we can code for it.
For Divisibility rules ...Refer :- math.about.com/library/bldivide.htm

- VolVo August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

ken.duisenberg.com/potw/archive/arch96/960919sol.html

- Lazy August 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yeah logic is fi9...bt its bit difficult to code... :(

- pks August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<iostream>
using namespace std;
void print(int a[],int prev_no ,int index,int inc[]); // 
int main()
{
    int a[9]={1,2,3,4,5,6,7,8,9};
    int inc[9]={0}; // this will take care of which no has been already used
    print(a,0,1,inc);
    system("pause");
}

void print(int a[],int prev_no ,int index,int inc[])
{
   
          if(index==10)   // if index is 10 means 9 digits with given constraint has been formed
          cout<<prev_no<<endl;
          for(int i=0;i<9;i++)  // one by one check whole no at this index
          {
                  if(inc[i]==0)
                  {
                               inc[i]=1; //use this no
                               
                               int t=( prev_no*10 +a [i] );  
                               if( ( t % index)==0)  // divisibility check of this new formed no
                               {
                                   print(a, t, index+1, inc);   //if divisible then check if furthur divisibility can be maintained 
                                   
                                   }
                                   inc[i]=0; // exclude this no
                                   }
                                   }
                                   }

- rockhammer August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Generate9DigitNumber {

	public static void main(String[] s) {
		int number = generateNumber(9, "");
		String str_num = new Integer(number).toString();
		if (str_num.length() != 9) {
			System.out.println("Required number not found!");
		} else
			System.out.println(str_num);
	}

	static int generateNumber(int digits, String number) {
		System.out.println(number);
		if (digits == 0)
			return Integer.parseInt(number);
		else if (digits == 1) {
			for (int i = 1; i <= 9; i++) {
				String temp = number + i;
				Integer temp_int = Integer.parseInt(temp);
				if (temp_int % (9 - digits + 1) == 0)
					return temp_int;
			}
			// Rite digit not found
			return Integer.parseInt(number);
		} else {
			for (int i = 1; i <= 9; i++) {
				String temp = number + i;
				Integer temp_int = Integer.parseInt(temp);
				if (temp_int % (9 - digits + 1) == 0) {
					return generateNumber(digits - 1, temp);
				}
			}
			return Integer.parseInt(number);
		}
	}
}

}

- DeathEater August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The idea is simple: A restricted permutation. Unlike checking all possible permutations of the 9 digits, I follow a strategy in which I only consider those possible options of the digits in the current method call, which satisfy a particular criteria, which is that the digit in current consideration, along with the earlier digits, is divisible by a desired number between 1 and 9.

- DeathEater August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Answer is 381654729

- Psycho August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
List<String> numbers=new ArrayList<String>();
public static void main(String args[]){
Test test=new Test();
test.generateNumber("",1);
test.print();
}
public void print(){
System.out.println("The numbers are:");
for(int l=0;l<this.numbers.size();l++)
System.out.println(""+(l+1)+": "+this.numbers.get(l));
}
public void generateNumber(String str,int mode){
String prevStr=str;
if(str.length()==9){
numbers.add(str);
return;
}
for(int l=1;l<=9;l++){
str+=String.valueOf(l);
if(Integer.parseInt(str)%mode==0)
generateNumber(str,mode+1);
str=prevStr;
}
}
}

The code will generate all possible numbers.The complexity would be n2.

- Rahul August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import SubSequence.MaxAdditionSubSequence;
import Tree.BinarySearchTree;
import Tree.BinaryTree;
import Utility.MaxSubSequence;


public class Test {
List<String> numbers=new ArrayList<String>();
public static void main(String args[]){
Test test=new Test();
test.generateNumber("",1);
test.print();
}
public void print(){
System.out.println("The numbers are:");
for(int l=0;l<this.numbers.size();l++)
System.out.println(""+(l+1)+": "+this.numbers.get(l));
}
public void generateNumber(String str,int mode){
String prevStr=str;
if(str.length()==9){
numbers.add(str);
return;
}
for(int l=1;l<=9;l++){
if(!str.contains(String.valueOf(l))){
str+=String.valueOf(l);
if(Integer.parseInt(str)%mode==0)
generateNumber(str,mode+1);
str=prevStr;
}
}
}
}

- If repetitions are not allowed. August 12, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

A faster way to search such a number.

#include <iostream>
bool div_7(int *d);
void print_num(char *mark, int *d);
int main (int argc, char * const argv[]) {
    
    char * mark=new char [10];
	memset(mark, 0, 10);
	
	int d[10];
	d[5]=5;
	mark[5]=1;
	
	for (int i_78=16; i_78<100; i_78+=8)
		if (i_78%10!=0&&i_78!=88&&i_78!=56)
		{
			d[7]=i_78/10;
			d[8]=i_78%10;
			mark[d[7]]=1;
			mark[d[8]]=1;
			
			
			
			for (int i_34=12;i_34<100;i_34+=4)
			if (i_34%10!=0&&i_34!=44&&i_34!=88)
			{
				d[3]=i_34/10;
				d[4]=i_34%10;
				if (mark[d[3]]||mark[d[4]]) continue;
				mark[d[3]]=1;
				mark[d[4]]=1;
				
				for (int i_2=2;i_2<10;i_2+=2)
					if (!mark[i_2])
					{
						d[2]=i_2;
						mark[i_2]=1;
						
						for (int i_1=1;i_1<10;i_1++)
							if (!mark[i_1]&&
								(i_1+d[2]+d[3])%3==0)
							{
								d[1]=i_1;
								mark[i_1]=1;
								
								for (int i_6=1;i_6<10;i_6++)
									if (!mark[i_6])
									{
										d[6]=i_6;
										mark[i_6]=1;

										if (div_7(d))
										{
											
											print_num(mark, d);
											
										}
										
										mark[i_6]=0;
									}
										
								
								mark[i_1]=0;
							}
						
						mark[i_2]=0;
					}
				
				mark[d[3]]=0;
				mark[d[4]]=0;
			}
			
			
			mark[d[7]]=0;
			mark[d[8]]=0;
		}
	
	return 0;
}

bool div_7(int *d)
{
	int t=0;
	int t_1=0;
	for (int i=1;i<=7;i++)
	{
		t=(t*10+d[i]);
		if (i<=6)
			t_1=(t_1*10+d[i]);
	}

	if (t%7==0&&t_1%6==0) return true;
	return false;
}

void print_num(char *mark, int *d)
{

	for (int i=1;i<=8;i++)
		std::cout<<d[i];
	for (int i=1;i<10;i++)
		if (!mark[i])
			std::cout<<i<<"\n";
}

- hwer.han August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

If the given numbers are 1-9 then looks like there is no solution for this. Divisiblity rule of 2 says, the first digit must be an even number (2,4,6,8) and divisibility rule of 5 says the first should be either 0 or 5. Zero (0) is not part given numbers and 5 can't be the first digit. So no solution. If the given set 0-9, then we may have one. Working on it :)

- Ashok August 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the set is 0-9 then, its too easy to find out 10 digit number which meets the above condition. 1987654320.

- Ashok August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ashok,

first 3 digits "320" are not divisible by 3 (for right to left)
if you consider left to right then "19" is not divisble by 2.....Can you check simple test case before posting?

- lazy August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry guys.. Mis-understood the question. It says first 3, but I read it as 1st and 3. But I believe the my first comment will still be valid??

- Ashok August 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

abe sahi hai''

- harry August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Abbé, bhai log Asok theek Bol raha hai..

- Prashant R Kumar August 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Initial case:
xxxx xxxx x

First 9 digits must be div by 9. 1-8 are not div by 9. If we choose 9, then 2nd dig is not div by 9. 0 is not allowed. Hence, this problem does not have a solution by counter-example.

- Yev August 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

29 is not divisible by 2.

- prairieon August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

Very simple , The magical Number is 123456789 , here 12 is divible by 2, 123 by 3, 1234 by 4 and so on.......

- Gaurav Garg August 13, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

1234/4 = ??? 1234567/7 =?? 12345678/8 = ??

- cobra August 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

there can be many number ...one of the possible number is
963258174

- jai August 16, 2012 | 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