Amazon Interview Question for SDE-2s


Country: United States
Interview Type: In-Person




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

def step_num_count(n, m, num):
	count  = 0
	q = []
	q.append(num)
	while q:
		step_num = q.pop(0)
		if step_num >= n and step_num <= m and (step_num/10):
			count += 1
		if step_num  is 0 or step_num > m:
			continue
		last_digit = step_num % 10
		step_num1 = (step_num * 10) + last_digit + 1
		step_num2 = (step_num * 10) + last_digit - 1
		if last_digit is 0:
			q.append(step_num1)
		elif last_digit is 9:
			q.append(step_num2)
		else:
			q.append(step_num1)
			q.append(step_num2)
	return count		
			
def main():
	count = 0
	n = 0
	m = 21
	for i in range(10):
		count += step_num_count(n, m, i)
	print "Total stepping num :",count
	
main()

- montu March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Missed out few points from qn. Corrected in this upload

def step_num_count(n1, n2, num):
	count  = 0
	q = []
	q.append(num)
	while q:
		step_num = q.pop(0)
		if step_num >= n1 and step_num <= n2 and (step_num/10):
			count += 1
		if step_num  is 0 or step_num > n2:
			continue
		last_digit = step_num % 10
		step_num1 = (step_num * 10) + last_digit + 1
		step_num2 = (step_num * 10) + last_digit - 1
		if last_digit is 0:
			q.append(step_num1)
		elif last_digit is 9:
			q.append(step_num2)
		else:
			q.append(step_num1)
			q.append(step_num2)
	return count		
			
def main():
	count = 0
	n1 = input("Enter value of n1:")
	n2 = input("Enter value of n2:")
	for i in range(10):
		count += step_num_count(n1, n2, i)
	print "Total stepping num :",count
	
main()

- montu March 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
					
public class Program
{
	public static void Main()
		
	{
		int n= 10 ;
		int m = 100;
		Console.WriteLine(DisplaySteppingNumber(n ,m));
	}
	
	public static int DisplaySteppingNumber(int n ,int m)
	{
		int count = 0;
		for (int i = 1 ; i <= 9 ; i++)
		{
			findSteppingNum( n , m , i , ref count);
		}
		
		return count;
	}
	public static void findSteppingNum (int n , int m , int stepnum , ref int count)
	{
		if ( stepnum > m || stepnum == 0 )
			
		{
			return ;
		}
		if (stepnum > n )
		{
			count ++;
		}
		 int lastDigit = stepnum % 10;
 
		// There can be 2 cases either digit
		// to be appended is lastDigit + 1 or
		// lastDigit - 1
		int stepnumA = stepnum*10 + (lastDigit-1);
		int stepnumB = stepnum*10 + (lastDigit+1);
		
		
		// If lastDigit is 0 then only possible
		// digit after 0 can be 1 for a Stepping
		// Number
		if (lastDigit == 0)
			findSteppingNum(n, m, stepnumB , ref count);
 
		// If lastDigit is 9 then only possible
		// digit after 9 can be 8 for a Stepping
		// Number
		else if(lastDigit == 9)
        findSteppingNum(n, m, stepnumA , ref count);
		else
		{
			 findSteppingNum(n, m, stepnumA , ref count);
			 findSteppingNum(n, m, stepnumB , ref count);
		}
	}
}

- Same Algorithm as above answer only using c# April 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
					
public class Program
{
	public static void Main()
		
	{
		int n= 10 ;
		int m = 100;
		Console.WriteLine(DisplaySteppingNumber(n ,m));
	}
	
	public static int DisplaySteppingNumber(int n ,int m)
	{
		int count = 0;
		for (int i = 1 ; i <= 9 ; i++)
		{
			findSteppingNum( n , m , i , ref count);
		}
		
		return count;
	}
	public static void findSteppingNum (int n , int m , int stepnum , ref int count)
	{
		if ( stepnum > m || stepnum == 0 )
			
		{
			return ;
		}
		if (stepnum > n )
		{
			count ++;
		}
		 int lastDigit = stepnum % 10;
 
		// There can be 2 cases either digit
		// to be appended is lastDigit + 1 or
		// lastDigit - 1
		int stepnumA = stepnum*10 + (lastDigit-1);
		int stepnumB = stepnum*10 + (lastDigit+1);
		
		
		// If lastDigit is 0 then only possible
		// digit after 0 can be 1 for a Stepping
		// Number
		if (lastDigit == 0)
			findSteppingNum(n, m, stepnumB , ref count);
 
		// If lastDigit is 9 then only possible
		// digit after 9 can be 8 for a Stepping
		// Number
		else if(lastDigit == 9)
        findSteppingNum(n, m, stepnumA , ref count);
		else
		{
			 findSteppingNum(n, m, stepnumA , ref count);
			 findSteppingNum(n, m, stepnumB , ref count);
		}
	}
}

- mohamadreza.shakouri April 06, 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