Amazon Interview Question
Front-end Software EngineersCountry: India
Interview Type: Written Test
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;
}
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);
}
}
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);
}
}
Nice Answer Dude ..But i mentioned about the ALGORITHM,u forgot that..anyway gooddddddddddd
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";
}
}
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]++;
}
}
}
#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;
}
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;
Implementing a hexdec counter.
Assumption: Max value is FFFFFFFFF The counter will reset when it reaches this max value.
- sd September 18, 2013