Amazon Interview Question for Front-end Software Engineers


Country: India
Interview Type: Written Test




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

Implementing a hexdec counter.
Assumption: Max value is FFFFFFFFF The counter will reset when it reaches this max value.

char IncrementChar(char ch)
{
	switch (ch)
	{
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	case '8':
		return (char) ((int) ch + 1);
		break;
	case '9':
		return 'A';
                break;
	case 'A':
	case 'B':
	case 'C':
	case 'D':
	case 'E':
		return (char) ((int) ch + 1);
		break;
	case 'F':
		return '0';
		break;
	default:
		return ' ';
		break;
	}	
}

char* IncrementString (char *str)
{
	if( !strcmp(str,"FFFFFFFFF"))
		return "000000000"; // Reset the counter after it reaches max

	for (int i=8; i>=0;i--)
	{
		str[i] = IncrementChar(str[i]);
		if ( str[i] != '0') 
			return str;
	}
	return str;
}

int _tmain(int argc, _TCHAR* argv[])
{

	char num[10] = "000000000";
        // Print value till FFF
	while(strcmp(num,"000000FFF") != 0)
	printf("%s\n" , IncrementString(num));
	return 0;
}

- sd September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Is it hexadecimal ?. If it isnt then the max value of counter be ZZ9ZZ9999

- Anonymous September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The given string contains Hex values hence convert it to Decimal values fist then increment the value again convert it back to Hex. You will get the answer. Simple.

- Arun September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

create a map from single "digit" n to n+1
increase the tail digit, if overflow, recursively increase the previous digit.
The solution can handle unlimited length strings.
js code:

var map = {
    0: "1",
    1: "2",
    ...
    C: "D",
    D: "0"
}
function increase(str) {
	var array = [];
	for(var i=str.length-1; i >= 0;i--){
		array.push(str.charAt(i));
	}
	array = increaseAt(array, 0);
	
    return  array.reverse().join("");
}

function increaseAt(array, index) {
	var d = array[index];
	var next = map[d];
	array[index] = next;
	
	if(next === "0") {// overflow
		var nextIndex = index+1;
		if(nextIndex  >= array.length) {
			array.push("1");
		} else {
			array = increaseAt(array, nextIndex);
		}
	}
	return array;
}

- Anonymous September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Very ambigious question . Please provide with proper requirements

- Chris September 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static String couterString(String s, int length){
char[] schar = s.toCharArray();
char c = schar[length-1];
if(c == '9'){
c = 'A';
schar[length -1] = c;
return String.valueOf(schar,0,length);
}else if(c == 'F'){
c= '0';
return couterString(s, length-1) + String.valueOf(c);
}else{
c++;
schar[length -1] = c;
return String.valueOf(schar,0,length);
}
}

- Recursive solution September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

assuming that character values will be unchanged, i.e. max value will be AB9CD9999

public class StringCounter {

	public static void main(String[]args)
	{
		String input = "AB0CD00000", tmp ="", result="";
		
		int number = 0;
		
		for(int i=0;i<input.length();i++)
		{
			if(Character.isDigit(input.charAt(i)))
			{
				tmp+=Character.toString(input.charAt(i));
			}
		}
		
		tmp ="1"+tmp;
		
		number = Integer.parseInt(tmp);
		
		
		if(number+1<2*Math.pow(10, tmp.length()-1))
			number++;
		
		tmp = Integer.toString(number);
		
		int j= 1;
		
		for(int i=0;i<input.length();i++)
		{
			if(Character.isDigit(input.charAt(i)))
			{
				result+= tmp.charAt(j);
				j++;
			}
			else
			{
				result+= input.charAt(i);
			}
		}
		
		System.out.println(result);
	}
}

- MacSan September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Nice Answer Dude ..But i mentioned about the ALGORITHM,u forgot that..anyway gooddddddddddd

- Anony September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice dude but i want to increment the character also for examples if input AB0CD9999 then the output is AB0CE0000 please help me

- Pragadees April 25, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public partial class Form1 : Form
{
ArrayList arr = null;
string sv = string.Empty;
public Form1()
{
InitializeComponent();
sv = "AB0CD1010";
int iv = sv.Length;
arr = new ArrayList();
foreach (char ch in sv)
{
arr.Add(ch);
}
}

private void button1_Click(object sender, EventArgs e)
{
int inum = 0;
bool bOnce = false;
for(int i=0;i<=arr.Count-1;i++)
{
bool bNum = int.TryParse(arr[i].ToString(), out inum);
if (bNum)
{
int iFirst=0;
bool bFirst = int.TryParse(arr[i - 1].ToString(), out iFirst);
if (bFirst && !bOnce)
{
int ival = Convert.ToInt32(arr[arr.Count - 1].ToString());
int Inc = ival + 1;
arr[arr.Count - 1] = Inc.ToString();
bOnce = true;
}
}
textBox1.Text += arr[i].ToString();
}
textBox1.Text += "\r\n";
}
}

- Aravind srinivas September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

char[] incrementString(char str[])
{
    int index = lengthOf(str)-1;
	str[index]++;
	while((str[index]==58 || str[index]==91) && index>=0) //58 is ASCII_9 + 1, 91 is ASCII_Z + 1
	{
		if(str[index] == 58) //If 1 is added to 9
		{
			str[index] = 65; // Set it to A
			return str;
		}
		if(str[index] == 91) //If 1 is added to Z
		{
			str[index] = 48; // Set it to 0
			if(index == 0)
			{
				char incStr[lengthOf(str)+1];
				incStr = concatenateCharToArray('1', str); //Its trivial to write this function. It shall be omitted. For example if str is ABC234 All it does is retrun 1ABC234
				return incStr;
			}
			else
				str[--index]++;
		}

	}
}

- Itanium September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is assuming the string digits start from 0-9-A-Z.so its like this
0000....0009....000A.....000Z.....0010......001Z.....002Z............0ZZZ........1000...........ZZZZ........10000 etc....

- Itanium September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the poster should clarify whether he has hex or is it like the one I am taking about in the above code snippet

- Itanium September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<string>
#include<iostream>
using namespace std;

int increment(int i)
{

if(i=='f')
return 0;
else
return (i+1);

}
int main()
{

string a;

int i=0,j;
cout<< "enter the string"<<endl;
cin>>a;

int k=a.length();

while(i<k)
{


j=increment((int)a[k-i-1]);

a[k-i-1]=(char)(j);
if(j==0)
{
a[k-i-1]=(char)48;
i++;
}
else break;
}
cout<<a<<endl;
return 0;
}

- Anonymous September 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Increment(string &s)
{
	if(s.size()==0)
		return;
	int n=s.size()-1;

	for(int i=n;i>=0;i--)
	{
		if(s[i]-'0'>=0 && s[i]-'0'<=8)
		{
			s[i]=((s[i]-'0')+1)+'0';
			break;
		}
		else if(s[i]=='9')
		{
			s[i]='A';
			break;
		}
		else if(s[i]>='A' && s[i]<='E')
		{
			s[i]++;
			break;
		}
		else if(s[i]=='F')
		{
			s[i]='0';
			if(i==0)
			{				
				s='F'+s;
			}
		}
	}
}

int main()
{
	string s="0";//"AB0CD1010";

	for(int i=0;i<300;i++)
	{
		Increment(s);
		cout<<s<<endl;
	}

	return 1;

- Anonymous September 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Number(parseInt("AB0CD1010",16)+1).toString(16)

- chetan December 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Number(parseInt("AB0CD1010",16)+1).toString(16)

- chetan December 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Number(parseInt("AB0CD1010",16)+1).toString(16)

- chetan December 21, 2015 | 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