Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

#include<iostream>
using namespace std;
int check(int a)
{
int flag=0;
int temp1,temp2;
while(a>0)
{
temp1=a%10;
if(a/10>0)
{
temp2=(a/10)%10;
int res=temp1-temp2;
if(res<0)
res=-res;
if(res>=0&&res<=1)
flag=0;
else
flag=1;
}
a=a/10;
}
if(flag==0)
return 1;
else
return 0;	
	
	
	
}

int main()
{
	int t;
	int n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int j=1;j<n;j++)
		{
			if(check(j))
			cout<<j<<"\n";
			
		}
	}
	return 0;
}

- Adithya June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
using namespace std;
int check(int a)
{
int flag=0;
int temp1,temp2;
while(a>0)
{
temp1=a%10;
if(a/10>0)
{
temp2=(a/10)%10;
int res=temp1-temp2;
if(res<0)
res=-res;
if(res>=0&&res<=1)
flag=0;
else
flag=1;
}
a=a/10;
}
if(flag==0)
return 1;
else
return 0;	
	
	
	
}

int main()
{
	int t;
	int n;
	cin>>t;
	while(t--)
	{
		cin>>n;
		for(int j=1;j<n;j++)
		{
			if(check(j))
			cout<<j<<"\n";
			
		}
	}
	return 0;
}

- Adithya June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{//code in c by nazeer email:nazeershaik95@gmail.com #include <stdio.h> #include <stdlib.h> void is_jumbleno(int ,int ,int ); int main() { int n,head,tail=0,res,remain,given,head1,i; printf("Enter a minimum 2 digit number for jumble no range :"); scanf("%d",&n); for(i=10;i<=n;i++) { given=i; head=i%10; head1=i/10; tail=head1%10; res=head-tail; remain =i/10; if(abs(res)==1) { if((remain/10)!=0) { is_jumbleno(remain,tail,given); } else if((remain/10)==0) { printf("%d \n",given); } /*else { printf("%d is not jumble number",given); }*/ } /*else { printf("%d is not jumble number",given); }*/ } return 0; } void is_jumbleno(int remain,int tail ,int given) { int head,res,head1; head1=remain/10; head=head1%10; res=head-tail; tail=head; head=remain; remain=head/10; if(abs(res)==1) { if((remain/10)!=0) { is_jumbleno(remain,tail,given); } else if ((remain/10)==0) { printf("%d \n",given); } /*else { printf("%d is jumble number"); }*/ } /*else { printf("%d is not jumble number",given); }*/ } - nazeer June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
void is_jumbleno(int ,int ,int );
int main()
{
    int n,head,tail=0,res,remain,given,head1,i;
    printf("Enter a minimum 2 digit number for jumble no range :");
    scanf("%d",&n);
    for(i=10;i<=n;i++)
    {
    given=i;
    head=i%10;
    head1=i/10;
    tail=head1%10;
    res=head-tail;
    remain =i/10;
    if(abs(res)==1)
    {
        if((remain/10)!=0)
        {
            is_jumbleno(remain,tail,given);
        }
        else if((remain/10)==0)
        {
           printf("%d \n",given);
 
        }
        /*else
        {
            printf("%d is not jumble number",given);
        }*/
    }
    /*else
    {
        printf("%d is not jumble number",given);
    }*/
    }
 
    return 0;
}
void is_jumbleno(int remain,int tail ,int given)
{
    int head,res,head1;
    head1=remain/10;
    head=head1%10;
    res=head-tail;
    tail=head;
    head=remain;
    remain=head/10;
    if(abs(res)==1)
    {
        if((remain/10)!=0)
        {
            is_jumbleno(remain,tail,given);
        }
        else if ((remain/10)==0)
        {
            printf("%d \n",given);
        }
        /*else
        {
            printf("%d is jumble number");
        }*/
    }
    /*else
    {
        printf("%d is not jumble number",given);
    }*/
 
}

- nazeer June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
void is_jumbleno(int ,int ,int );
int main()
{
int n,head,tail=0,res,remain,given,head1,i;
printf("Enter a minimum 2 digit number for jumble no range :");
scanf("%d",&n);
for(i=10;i<=n;i++)
{
given=i;
head=i%10;
head1=i/10;
tail=head1%10;
res=head-tail;
remain =i/10;
if(abs(res)==1)
{
if((remain/10)!=0)
{
is_jumbleno(remain,tail,given);
}
else if((remain/10)==0)
{
printf("%d \n",given);

}
/*else
{
printf("%d is not jumble number",given);
}*/
}
/*else
{
printf("%d is not jumble number",given);
}*/
}

return 0;
}
void is_jumbleno(int remain,int tail ,int given)
{
int head,res,head1;
head1=remain/10;
head=head1%10;
res=head-tail;
tail=head;
head=remain;
remain=head/10;
if(abs(res)==1)
{
if((remain/10)!=0)
{
is_jumbleno(remain,tail,given);
}
else if ((remain/10)==0)
{
printf("%d \n",given);
}
/*else
{
printf("%d is jumble number");
}*/
}
/*else
{
printf("%d is not jumble number",given);
}*/

}

- nazeer June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

while n > 9:
    intToStr = str(n-1)
    n -= 1
    flag = True
    for i in range(len(intToStr)-1):
        if abs(int(intToStr[i]) - int(intToStr[i+1])) != 1:
            flag = False
            break

    if flag and n!= 9:
        print(intToStr)

- Python 3 June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class JumbledNo {

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1;i<n;i++)
{
boolean chk = false;
String x = String.valueOf(i);
int[] m = new int[x.length()];
for(int j=0;j<x.length();j++)
{
String h = String.valueOf(x.charAt(j));
m[j] = Integer.parseInt(h);
}
for(int j=0;j<x.length()-1;j++)
{
if((m[j]-m[j+1]==1) || (m[j+1]-m[j]==1))
{
chk = true;
}
}
if(chk==true)
{
System.out.println(i);
}
}
}
}

- Anonymous June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class JumbledNo {

	public static void main(String args[])
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(int i=1;i<n;i++)
		{
			boolean chk = false;
			String x = String.valueOf(i);
			int[] m = new int[x.length()];
			for(int j=0;j<x.length();j++)
			{
				String h = String.valueOf(x.charAt(j));
				m[j] = Integer.parseInt(h);
			}			
			for(int j=0;j<x.length()-1;j++)
			{
				if((m[j]-m[j+1]==1) || (m[j+1]-m[j]==1))
				{
					chk = true;
				}
			}
			if(chk==true)
			{
				System.out.println(i);
			}
		}
	}
}

- Anonymous June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NumberJumble {

	public static void main(String[] args) {
		
		boolean flag = jumbleOrNot(13477);
		if(flag) {
			System.out.println("This is a jumbled number");
		} else {
			System.out.println("This is not a jumbled number");
		}

	}
	
	public static boolean jumbleOrNot(Integer number) {
		Integer previous_digit = 0;
		Integer current_digit = 0;
		boolean flag = true;
		while(number > 0) {
			previous_digit = current_digit;
			current_digit = number % 10;
			number = number / 10;
			if(previous_digit != 0) {
				if(previous_digit == (current_digit + 1) || previous_digit == (current_digit -1 )) {
					continue;
				} else {
					flag = false;
					break;
				}
			}	
		}
		return flag;
	}

}

- Sridhar June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class NumberJumble {

	public static void main(String[] args) {
		
		boolean flag = jumbleOrNot(13477);
		if(flag) {
			System.out.println("This is a jumbled number");
		} else {
			System.out.println("This is not a jumbled number");
		}

	}
	
	public static boolean jumbleOrNot(Integer number) {
		Integer previous_digit = 0;
		Integer current_digit = 0;
		boolean flag = true;
		while(number > 0) {
			previous_digit = current_digit;
			current_digit = number % 10;
			number = number / 10;
			if(previous_digit != 0) {
				if(previous_digit == (current_digit + 1) || previous_digit == (current_digit -1 )) {
					continue;
				} else {
					flag = false;
					break;
				}
			}	
		}
		return flag;
	}

}

- Sridhar June 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printJumbled(int n){

        // Happy Path

        //10,12,21,
//        n >10
        // Error
        /* 0 < n <10
        n<=0
        * */
        if (n<0){
            System.out.println("Negative values, Please provide a positive value");
        }
        else if ( n==0){
            System.out.println(n);

        } else if(n<11 && n>0){

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

        }

        for (int i = 12; i <n;i++) {
            if(isJumbled(i)){
                System.out.println(i);
            }
        }
    }

    static boolean isJumbled(int n){
        //store it in a Array
        // i might first element
        //i might last element
        // i might be middle element
        String intJmb = String.valueOf(n);

        for (int i = 0; i <intJmb.length() ; i++) {

            if(i<intJmb.length()-1 && i>0){

                int right = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i+1)));
                int left = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i-1)));
                if(right!=1 && left !=1){
                    return false;
                }
            }else if (i==0 && i <intJmb.length()){
                int right = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i+1)));
                if(right!=1 ){
                    return false;
                }

            }else if (i>0 && i ==intJmb.length()-1){
                int left = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i-1)));
                if(left !=1){
                    return false;
                }

            }
        }

- Sridhar Jayapaul June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printJumbled(int n){

        // Happy Path

        //10,12,21,
//        n >10
        // Error
        /* 0 < n <10
        n<=0
        * */
        if (n<0){
            System.out.println("Negative values, Please provide a positive value");
        }
        else if ( n==0){
            System.out.println(n);

        } else if(n<11 && n>0){

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

        }

        for (int i = 12; i <n;i++) {
            if(isJumbled(i)){
                System.out.println(i);
            }
        }
    }

    static boolean isJumbled(int n){
        //store it in a Array
        // i might first element
        //i might last element
        // i might be middle element
        String intJmb = String.valueOf(n);

        for (int i = 0; i <intJmb.length() ; i++) {

            if(i<intJmb.length()-1 && i>0){

                int right = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i+1)));
                int left = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i-1)));
                if(right!=1 && left !=1){
                    return false;
                }
            }else if (i==0 && i <intJmb.length()){
                int right = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i+1)));
                if(right!=1 ){
                    return false;
                }

            }else if (i>0 && i ==intJmb.length()-1){
                int left = Math.abs(Integer.valueOf(intJmb.charAt(i)) - Integer.valueOf(intJmb.charAt(i-1)));
                if(left !=1){
                    return false;
                }

            }

}

- sridharjayapaul June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class JumbledNumberChecker {
	
	private static boolean isJumbledNumber(int number) {
		while(number % 10 != 0){
			int digit1 = number % 10;
			int digit2 = (number/10) % 10;
			if(Math.abs(digit2-digit1) > 1 && digit2 != 0)
				return false;
			number /= 10;
		}
		return true;
	}

	public static void main(String[] args) {
		int n1 = -1234;
		int n2 = 8987;
		int n3 = 13;
		int n4 = 287;
		System.out.println("Is " + n1 + " jumbled number? : "
				+ isJumbledNumber(n1));
		System.out.println("Is " + n2 + " jumbled number? : "
				+ isJumbledNumber(n2));
		System.out.println("Is " + n3 + " jumbled number? : "
				+ isJumbledNumber(n3));
		System.out.println("Is " + n4 + " jumbled number? : "
				+ isJumbledNumber(n4));
	}
}

- vishal naik June 14, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void diff(int n)
	{
		String s,c;
		String s1=" ",s2=" ";
		boolean flag=true;
	for(int i=10;i<=n;i++)
	{
		// Convert the number to string
		s=Integer.toString(i);
		for(int j=0;j<s.length()-1;j++)
		{
			//For each number 0 to N compare the values of adjacent integer in N
		    c=String.valueOf(i);
		    s1=String.valueOf(c.charAt(j));
		    s2=String.valueOf(c.charAt(j+1));
			if(Integer.parseInt(s1)==Integer.parseInt(s2)+1 || Integer.parseInt(s1)+1 == Integer.parseInt(s2))
			{
				flag=true;
				continue;
			}
			else
			{
				flag=false;
				break;
			}
			
		}
		if(flag)
		{
			System.out.println(i);	
		}
		
	}
	}

- siva June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindJumbled{
    
    public static boolean isJumbled(int n)
    {
        boolean flag = false;
    	while(n>9){
    		int ithDigit = n%10;
    		n = n/10;
    		int ithMinusOneDigit = n%10;
    	//	System.out.println(ithDigit+","+ithMinusOneDigit);
    		int diff = ithDigit - ithMinusOneDigit;
    		if(diff == 1 || diff == -1){
    			flag=true;
    		}else{
    		    flag=false;
    		    break;
    		}
    		
    	}
    	return flag;
    }
     public static void main(String []args){
        int N = 9999;
         
        for(int i =1; i<N;i++){
            if(isJumbled(i)){
                System.out.println(i);
            }
        }
     }
}

- Ashish Panery June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FindJumbled{
    
    public static boolean isJumbled(int n)
    {
        boolean flag = false;
    	while(n>9){
    		int ithDigit = n%10;
    		n = n/10;
    		int ithMinusOneDigit = n%10;
       		int diff = ithDigit - ithMinusOneDigit;
    		if(diff == 1 || diff == -1){
    			flag=true;
    		}else{
    		    flag=false;
    		    break;
    		}
    		
    	}
    	return flag;
    }
     public static void main(String []args){
        int N = 9999;
         
        for(int i =1; i<N;i++){
            if(isJumbled(i)){
                System.out.println(i);
            }
        }
     }
}

- Ashish Panery June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class JumbledNumbers {
	public static void main(String args[])	{
		String number = "2222134";
		//int i = -1 % 10;
		printJumbledNumbers(number);
	}

	private static void printJumbledNumbers(String number) {
		// TODO Auto-generated method stub
		for ( int i = 1 ; i <= 9 ; i++){
			String str = "";
			str= str + i;
			printJumbedNumbersUtil(str , number);
		}
	}

	private static void printJumbedNumbersUtil(String str, String number) {
		// TODO Auto-generated method stub
		if ( Long.valueOf(str) <= Long.valueOf(number) )	{
			System.out.println(str);
			
			String incrementStr = str;
			int nextDigitIncrement  = Integer.valueOf(str.substring(str.length() - 1 , str.length() )) + 1;
			if ( nextDigitIncrement != 10)	{
				incrementStr = incrementStr + nextDigitIncrement;
				printJumbedNumbersUtil(incrementStr , number);
			}
			
			String decrementStr = str;
			int nextDigitDecrement = Integer.valueOf(str.substring(str.length() - 1)) - 1;
			if (nextDigitDecrement !=  -1)	{
				decrementStr = decrementStr + nextDigitDecrement;
				printJumbedNumbersUtil(decrementStr , number);
			}
	}
		else {
			return;
		}
		
	}
}

- Sibendu Dey June 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int findDigits(int n) {
int i = 0;
while (n > 0) {
n = n / 10;
i++;
}
return i;
}

int main()
{
int n, number, seed, digits, msb, indice, i;
cin >> n;
random_device rd;
srand(time(NULL));
seed = (rand() % 10);
number = seed;
while(number < n) {
digits = findDigits(number);
msb = number/pow(10,digits-1);
indice = rand() % 2;
indice ? i = msb + 1 : i = msb - 1;
number = number + i*pow(10, digits);
}
if (number > n)
number = number / 10;
cout << number << "\n";
}

- Ashish Shavarna June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int findDigits(int n) {
	int i = 0;
	while (n > 0) {
		n = n / 10;
		i++;
	}
	return i;
}

int main()
{
	int n, number, seed, digits, msb, indice, i;
	cin >> n;
	random_device rd;
	srand(time(NULL));
	seed = (rand() % 10);
	number = seed;
	while(number < n) {
		digits = findDigits(number);
		msb = number/pow(10,digits-1);
		indice = rand() % 2;
		indice ? i = msb + 1 : i = msb - 1;
		number = number + i*pow(10, digits);
	}
	if (number > n)
		number = number / 10;
	cout << number << "\n";
}

- Ashish Shavarna June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int findDigits(int n) {
	int i = 0;
	while (n > 0) {
		n = n / 10;
		i++;
	}
	return i;
}

int main()
{
	int n, number, seed, digits, msb, indice, i;
	cin >> n;
	random_device rd;
	srand(time(NULL));
	seed = (rand() % 10);
	number = seed;
	while(number < n) {
		digits = findDigits(number);
		msb = number/pow(10,digits-1);
		indice = rand() % 2;
		indice ? i = msb + 1 : i = msb - 1;
		number = number + i*pow(10, digits);
	}
	if (number > n)
		number = number / 10;
	cout << number << "\n";
}

- Ashish Shavarna June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <random>
#include <ctime>
using namespace std;

int findDigits(int n) {
	int i = 0;
	while (n > 0) {
		n = n / 10;
		i++;
	}
	return i;
}

int main()
{
	int n, number, seed, digits, msb, indice, i;
	cin >> n;
	random_device rd;
	srand(time(NULL));
	seed = (rand() % 10);
	number = seed;
	while(number < n) {
		digits = findDigits(number);
		msb = number/pow(10,digits-1);
		indice = rand() % 2;
		indice ? i = msb + 1 : i = msb - 1;
		number = number + i*pow(10, digits);
	}
	if (number > n)
		number = number / 10;
	cout << number << "\n";

}

- Ashish Shavarna June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AmazonJumbledNumber {

	public static void main(String[] args) {
		
		String input = "123456789";
		
		System.out.println(process (input));

	}
	
	private static boolean process (String input){
		
		int index = 0;
		
		while (index < input.length()-1  ) {
			
			int number = Integer.valueOf(input.substring(index, index+1));
			int numberTwo = Integer.valueOf(input.substring(++index, index+1));
			
			if (number+1 != (numberTwo  ) &&  number-1 != (numberTwo ) ){
				
				return false;
				
			}
			
		}
		
		return true;
		
	}

}

- israAzul June 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class JumbledNumbers {
    private final int[][] jumbledGraph = new int[][]{
            {0, 1, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
            {0, 1, 0, 1, 0, 0, 0, 0, 0, 0},
            {0, 0, 1, 0, 1, 0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0, 1, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0, 1, 0, 1, 0, 0},
            {0, 0, 0, 0, 0, 0, 1, 0, 1, 0},
            {0, 0, 0, 0, 0, 0, 0, 1, 0, 1},
            {0, 0, 0, 0, 0, 0, 0, 0, 1, 0}};

    public void printJumbledNumber(int n) {
        int num = n;
        int maxLetters = 0;
        while (num > 0) {
            maxLetters++;
            num /= 10;
        }
        for (int i = 2; i <= maxLetters; i++) {
            Stack<Integer[]> s = new Stack<>();
            //s.push(new Integer[]{0, 0, 0});
            s.push(new Integer[]{1, 1, 0});
            s.push(new Integer[]{2, 1, 0});
            s.push(new Integer[]{3, 1, 0});
            s.push(new Integer[]{4, 1, 0});
            s.push(new Integer[]{5, 1, 0});
            s.push(new Integer[]{6, 1, 0});
            s.push(new Integer[]{7, 1, 0});
            s.push(new Integer[]{8, 1, 0});
            s.push(new Integer[]{9, 1, 0});

            while (!s.isEmpty()) {
                Integer[] step = s.pop();
                int number = step[2] * 10 + step[0];
                if (step[1] < i) {
                    for (int k = jumbledGraph[step[0]].length - 1; k >= 0; k--) {
                        if (jumbledGraph[step[0]][k] == 1) {
                            s.push(new Integer[]{k, step[1] + 1, number});
                        }
                    }
                } else {
                    if(number < n) System.out.println(number);
                }
            }
        }
    }

    public static void main(String[] args) {
        JumbledNumbers jumbledNumbers = new JumbledNumbers();
        jumbledNumbers.printJumbledNumber(100000000);
    }
}

- shashank.neo June 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

List<string> output = new List<string>();

            int N = 6789;

            for (int i = 0; i < N; i++)
            {
                string strN = i.ToString();
                if (strN.Length > 1)
                {
                    bool result = true;
                    for (int j = 0; j < strN.Length - 1; j++)
                    {
                       int diff = Convert.ToInt32(strN[j + 1]) - Convert.ToInt32(strN[j]);
                       if (diff != 1 && diff != -1)
                       {
                           result = false;
                           break;
                       }
                    }
                    if (result)
                    {
                        output.Add(strN);
                        Debug.WriteLine(strN);
                    }
                }
            }

- Srithar91 June 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void getJumbledNumbers(int n) {
		int remainder = 0;
		int quotient = 0;
		boolean isJumbled = false;
		for (int i=1; i<n; i++) {
			quotient = i/10;
			remainder = i%10;
			while (quotient > 0) {
				if ((remainder - quotient%10) == 1 || (remainder - quotient%10 == -1)) {
					isJumbled = true;
					remainder = quotient%10;
					quotient = quotient/10;
				} else {
					isJumbled = false;
					break;
				}
			}
			if (isJumbled) {
				System.out.println(i);
			}
		}
	}

- Rakesh K Balavanthapu June 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void getJumbledNumbers(int n) {
		int remainder = 0;
		int quotient = 0;
		boolean isJumbled = false;
		for (int i=1; i<n; i++) {
			quotient = i/10;
			remainder = i%10;
			while (quotient > 0) {
				if ((remainder - quotient%10) == 1 || (remainder - quotient%10 == -1)) {
					isJumbled = true;
					remainder = quotient%10;
					quotient = quotient/10;
				} else {
					isJumbled = false;
					break;
				}
			}
			if (isJumbled) {
				System.out.println(i);
			}
		}
	}

- Rakesh.Balavanthapu June 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <bits/stdc++.h>
using namespace std;
int main (void)
{
 int n;
 vector <int > v;
 cin >> n;
 while (n--)
 {
     v.push_back ( n % 10);
     n = n / 10;
 }
 bool flg = 1;
 for (int i = 0 ; i < v.size ()-1 ; ++i)
 {
     if (abs(v[i] - v[i+1])> 1) flg = 0 ;
 }
 (flg) ? cout << "J" : cout << "Not j";
}

- Omar Behsary June 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is a brute force approach,evaluating all numbers from 0 to N-1 : -

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

public class JumbledNumbers {

public static void main(String[] args) {
List<Integer> jumbledNumbers = new ArrayList<Integer>();
int N = 100;
for (int i = 0; i < N; i++) {
if(isJumbled(i)){
jumbledNumbers.add(i);
}
}
System.out.println(jumbledNumbers);
}

public static boolean isJumbled(int num){
if(num < 10){
return false;
}else{
String numVal = String.valueOf(num);
for (int i = 0; i < numVal.length() - 1 ; i++) {
int digit1 = Integer.valueOf(numVal.charAt(i)+"");
int digit2 = Integer.valueOf(numVal.charAt(i+1)+"");
if( ( digit1 - digit2 == 1) || (digit2 - digit1 == 1)){
continue;
}else{
return false;
}
}
return true;
}
}
}

- ultikhopdi June 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

A recursive solution:

void print_jumbled(unsigned int n) {
	unsigned int i;
	for (i = 1; i < n; i++)
		if (jumbled(i))
			printf("%u ", i);
}

bool jumbled(unsigned int n) {
	if (n < 10)
		return true;

	unsigned int first = n % 10;
	n = n / 10;
	unsigned int second = n % 10;
	if ((first == second) || (first + 1 == second) || (first == second + 1))
		return jumbled(n);

	return false;
}

- Mukesh June 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is the solution using dynamic programming. It only calculates jumbled numbers from 10 to 99. Rest all answers can be obtained by using previous results. X is a jumbled number, if its last 2 digits are jumbled number, and first n-1 digits are jumbled numbers.

void print_jumbled(unsigned int N) {
	bool *a = (bool *) calloc (1, N);
	if (a == NULL) {
		printf("Failed to allocate memory.");
		return;
	}

	unsigned int i;
	for (i = 1; i < N; i++)
		if (jumbled(a, i))
			printf("%u ", i);

	free(a);
}

bool jumbled(bool a[], unsigned int n) {
	if (n < 10) {
		a[n] = true;
		return true;
	}

	if (n < 100) {
		unsigned int first = n % 10;
		unsigned int second = (n/10) % 10;
		if ((first == second) || (first + 1 == second) || (first == second + 1)) {
			a[n] = true;
			return true;
		}
		a[n] = false;
		return false;
	}

	a[n] = a[n % 100] && a[n/10];
	return a[n];
}

- Mukesh July 07, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string>
#include <stack>
#include <vector>
#include <cstdlib>

using namespace std;

void isJumbled(long long num)
{
    if (num == 0)
    {
        cout << "Num [" << num << "] is not jumbled" << endl;
        return;
    }

    int lastD = 0;
    unsigned long long uNum = llabs(num);

    do
    {
        if (lastD != 0 && llabs(lastD - uNum%10) != 1)
        {
            cout << "Num [" << num << "] is not jumbled" << endl;
            return;
        }

        lastD = uNum %10;
        uNum = uNum/10;
    } while (uNum > 0);
    
    cout << "Num [" << num << "] is jumbled" << endl;
}

int main()
{
    long long num = 0;
    cin >> num;
    
    isJumbled(num);
}

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

import java.io.*;
import java.util.*;
public class Jumb {

	public static int maxN = 0;

	public static void jumble(int ln){
	    int fn = 9;
	    if(ln == maxN){
	    	String maxS = Integer.toString(maxN);
	    	String ch = maxS.substring(0,1);
	    	fn = Integer.parseInt(ch);
	    	++fn;
	    	if(fn > 9){
	    		fn = 9;
	    	}
	    }
		
		for(int i=1; i<=fn; i++ ){	
			 jumbleHelp(i,ln, i);
		}
	}
	
	public static void jumbleHelp(int a,  int ln,  int temp){
		
		if(ln==1){
		    if(temp > maxN){
		    	return;
		    }
		    else{
		    	System.out.println(temp);
		    	//arr[arrL] = temp;
				//++arrL;
				return;	
		    }			
		}
		
		int b = a+1;
		int c = a-1;
		if(c >=0 && c<10){
        	jumbleHelp(c, ln-1,  (temp*10+c));
		}
		if(b >=0 && b<10){
		 	jumbleHelp(b, ln-1,  (temp*10+b));
		}
		
        
		
		return;
		
	}
	

	public static void main (String[] args) throws java.lang.Exception
    {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	    String line = br.readLine();
	    int ln = line.length();
	    int n = Integer.parseInt(line);
	    maxN = n;
	
	    int[] arr = new int[n];
	    for(int i=1; i<=ln; i++){
	    	jumble(i);
	    }
	 /*   for(int i=0; i<arrL;i++){
	    	System.out.println(arr[i]);
	    }*/
	    
    }
	
}

- sidhantshu July 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just iterate through each digit i and check if the value[i]-value[i-1] is +1 or -1 or 0.

- arviman August 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If not brute force, use BFS to generate numbers and maintain a counter and prune search when value > N.

- arviman August 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Time: O(N^(0.3) * lgN)
Space: O(lgN)

Recursive approach that doesn't iterate over 1..N but instead uses a double ended queue to store the jumbled numbers as they are being built.

import java.lang.Math;
import java.util.Deque;
import java.util.LinkedList;

public class Jumbled
{
  // arguments are passed using the text field below this editor
  public static void main(String[] args)
  {
    int N = 10000000;
    printJumbled(N);
  }
  
  public static void printJumbled(int N) {
    for (int i = 1; i <= 9; i++) {
      Deque<Integer> base = new LinkedList<Integer>();
      base.addFirst(i);
      printJumbled(base, N);
    }
  }
  
  private static void printJumbled(Deque<Integer> base, int N) {
    if (base.size() == digitsIn(N)) {
      if (base.peekFirst() != 0 && valueOf(base) < N) {
        System.out.println(valueOf(base));
      }
    } else if (base.size() < digitsIn(N)) {
      if (base.peekFirst() != 0) {
        System.out.println(valueOf(base));
      }
      
      for (int digit : getNeighboringDigits(base.peekFirst())) {
        base.addFirst(digit);
        printJumbled(base, N);
        base.removeFirst();
      }
    }
  }
  
  private static int valueOf(Deque<Integer> deq) {
    int place = 0;
    int value = 0;
    
    Integer[] arr = deq.toArray(new Integer[deq.size()]);
    
    for (int i = 0; i < arr.length; i++) {
      value += arr[arr.length - 1 - i] * Math.pow(10, i);
    }
    return value;
  }
  
  private static int digitsIn(int N) {
    return ((int) Math.log10(N)) + 1;
  }
  
  private static int[] getNeighboringDigits(int digit) {
    int left = digit - 1;
    int right = digit + 1;
    
    if (left >= 0) {
      if (right <= 9) {
        return new int[] {left, right};
      } else {
        return new int[] {left}; 
      }
    } else {
      if (right <= 9) {
        return new int[] {right};
      } else {
        return new int[] {};
      }
    }
  }
}

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

I understood wrongly. I thought we need to print all anagrams which are jumbled format.
Here is the code for the same.

public void jumbledAnagrams(int[] a, int index, int n) {
        if (index >= n - 1) {
            printArray(a);
        }

        for (int i = index + 1; i < n; i++) {
            if (areAdjacentNumbers(a[index], a[i])) {
                if (index + 1 != i) {
                    if (a[i] != a[index + 1]) {
                        swap(a, index + 1, i);
                        jumbledAnagrams(a, index + 1, n);
                    }
                } else {
                    jumbledAnagrams(a, index + 1, n);
                }
            }
        }
    }

- ranganath111 September 04, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package main

import (
	"fmt"
)

func nextJumbled(n int) int {
	if n < 10 {
		return n + 1
	}
	last := n % 10
	prev := (n / 10) % 10
	if last < 8 && prev > last {
		return n + 2
	}
	prefix := nextJumbled(n / 10)
	last = prefix % 10
	if last == 0 {
		return prefix*10 + 1
	}
	return prefix*10 + last - 1
}

func main() {
	max := 1000
	cur := 10
	for cur <= max {
		fmt.Printf("%d ", cur)
		cur = nextJumbled(cur)
	}
}

- dmitry.labutin October 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class JumbledNumber {


private static boolean isJumbledNumber(long n) {
int previous = (int)(n % 10);
n = n / 10;
while(n > 0) {
int current = (int)(n % 10);
n = n / 10;
if((current+1) != previous && (current-1) != previous){
return false;
}
previous = current;
}

return true;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
long m = scanner.nextLong();
System.out.println(isJumbledNumber(m));
}
}

- nsenthilrajaed June 12, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/**
 * Created by hasan on 26/7/17.
 */
public class test {

    public static void main(String args[]){
        String num = (String.valueOf(Math.abs((long)9878)));
        int len = num.length();
        int array[]=new int[len];

        for(int i=0;i<len;i++)

        {

            array[i]=Character.getNumericValue(num.charAt(i));
        }
        int flag=0;

        for(int i=0;i<=len;i++)
        {
            if((i+1)==len)
                break;
            else {
                if (((Math.abs(array[i] - array[i + 1])) != 1)) {
                    flag = 1;//not jumbled
                    continue;
                }
            }
        }
        if(flag==0)
            System.out.print("Number is jumbled");
        else
            System.out.print("Number is not jumbled");
    }
}

- hasanfaraaz2 July 26, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solution with Strings so that you don't care if the numbers are too big for long:

package com.careercup.ruslanbes.jumbled;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Main {
    public static String[] jumbledNumbers(final int n) {
        return listToArray(jumbledNumbersInternal(n, false));
    }

    protected static List<String> jumbledNumbersInternal(final int n, boolean leadingZeroes) {
        final String[] digits = {"0", "1", "2", "3", "4", "5", "6" ,"7", "8","9"};
        if (n == 1) {
            return Arrays.asList(digits);
        }

        final List<String> oldNumbers = jumbledNumbersInternal(n-1, true);
        final List<String> numbers = new LinkedList<>();
        final byte zero = (byte)'0';
        final byte one = zero+1;
        final byte nine = zero+9;
        for (String s: oldNumbers) {
            final byte c = (byte)s.charAt(0);
            if (c != zero || leadingZeroes) {
                numbers.add(String.valueOf((char)(c)) + s);
            }

            if (c > one || leadingZeroes && c > zero) {
                numbers.add(String.valueOf((char)(c-1)) + s);
            }

            if (c < nine) {
                numbers.add(String.valueOf((char)(c+1)) + s);
            }
        }
        return numbers;
    }

    protected static String[] listToArray(List<String> list) {
        String[] array = new String[list.size()];
        for(int i = 0; i < array.length; i++) {
            array[i] = list.get(i);
        }
        return array;
    }
}

- ruslanbes2 October 17, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

import Queue

def printJumbled(N):
    q = Queue.Queue(N);
    for idx in xrange(1, 10):
        q.put(idx);
    while not q.empty():
          item = q.get();
          print(item);
          lastElem = (item % 10);
          if lastElem > 1 and ((10 * item) + (lastElem - 1)) <= N:
             q.put((10 * item) + (lastElem - 1));
          if lastElem < 9 and ((10 * item) + (lastElem + 1)) <= N:
             q.put((10 * item) + (lastElem + 1));    
              
printJumbled(999);

- koustav.adorable July 26, 2017 | 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