Amazon Interview Question
Software Engineer / Developersimport java.util.*;
class Q1
{
public static String q1(int numerator, int denominator)
{
int whole = numerator/denominator;
int newNumerator = numerator - (whole * denominator);
if(newNumerator == 0){
//The numer is whole
return Integer.toString(whole);
}
HashMap numerators = new HashMap();
String decimalPart = "";
//Keeps track of the number we are calculating past the decimal point
int index = 0;
while(newNumerator != 0 && !numerators.containsKey(newNumerator)){
//Add the current numerator to the hashmap with the value at index
//So that we know when in the decimal string to insert parens it necessary
numerators.put(newNumerator, index);
if(newNumerator < denominator){
newNumerator *= 10;
}
int nextInt = newNumerator / denominator;
decimalPart += Integer.toString(nextInt);
newNumerator -= (nextInt * denominator);
index++;
}
//Check if we have a repeating part
if(newNumerator != 0){
//We have reached a repeating decimal value
//Check in the hashmap at what index the repition started
Integer rep = (Integer)numerators.get(newNumerator);
decimalPart = decimalPart.substring(0,rep) + "[" + decimalPart.substring(rep) + "]";
return Integer.toString(whole) + "." + decimalPart;
}
//If we are still here then there is no repeating decimal
return Integer.toString(whole) + "." + decimalPart;
}
}
import java.util.*;
class Q1
{
public static String q1(int numerator, int denominator)
{
int whole = numerator/denominator;
int newNumerator = numerator - (whole * denominator);
if(newNumerator == 0){
//The numer is whole
return Integer.toString(whole);
}
HashMap numerators = new HashMap();
String decimalPart = "";
//Keeps track of the number we are calculating past the decimal point
int index = 0;
while(newNumerator != 0 && !numerators.containsKey(newNumerator)){
//Add the current numerator to the hashmap with the value at index
//So that we know when in the decimal string to insert parens it necessary
numerators.put(newNumerator, index);
if(newNumerator < denominator){
newNumerator *= 10;
}
int nextInt = newNumerator / denominator;
decimalPart += Integer.toString(nextInt);
newNumerator -= (nextInt * denominator);
index++;
}
//Check if we have a repeating part
if(newNumerator != 0){
//We have reached a repeating decimal value
//Check in the hashmap at what index the repition started
Integer rep = (Integer)numerators.get(newNumerator);
decimalPart = decimalPart.substring(0,rep) + "[" + decimalPart.substring(rep) + "]";
return Integer.toString(whole) + "." + decimalPart;
}
//If we are still here then there is no repeating decimal
return Integer.toString(whole) + "." + decimalPart;
}
}
#include <iostream>
#include<stdlib.h>
#include <strstream>
using namespace std;
int *hashMap = NULL;
int denominator = 0;
int total = 10;
string printremainder(int quotient , int remainder) {
string str ="";
if(remainder==0)
return str;
hashMap[remainder]++;
if(hashMap[remainder] == 1)
{
strstream s ;
s<<quotient;
str = s.str();
if( total-- )
str += printremainder(remainder*10/denominator, remainder*10%denominator);
if(hashMap[remainder] == 1)
str = "[" + str;
}
else
{
str = "]";
}
hashMap[remainder]--;
return str;
}
int main() {
int num = 221;
denominator = 3;
hashMap = new int [denominator];
cout<<num/denominator;
if( num%denominator )
cout<<"."<<printremainder(((num - denominator* (int)(num/denominator))*10)/denominator, ((num - denominator* (int)(num/denominator))*10)%denominator);
delete [] hashMap;
return 0;
}
use sprintf.
- @rams May 24, 2011