Amazon Interview Question
Software Engineer / DevelopersTeam: TRMS
Country: United States
Language: C++
Strategy: use string append of cstrings to copy chunks of original string. Use modulo to figure out the number of digits before and after '.' notation. Use division to figure out which unit to use (B,K,M, or G).
Assumptions[room for improvement]:
- any character can be part of the string, not just numbers.
- no space in input string.
- each unit occupies only one byte.
#include <cstdlib>
#include <iostream>
#include <cmath>
#define SIZE 4
#define UPPERLIMIT 10 //we can only process up to 10 digits
#define NDIGITS 3 //number of digits that are readable, in our case only 3
char * readable (char * string);
int main (int argc, char ** argv){
char input []= "1000000000B"; //"341B" //"12345B" /
printf ("%s\n", readable(input));
}
char * readable (char * string) {
char order [] = "BKMG"; //the order
char * newString = new char (SIZE);
int size = strlen(string)-1;
if (string == NULL) {
printf ("string is empty");
exit (0);
}
if (size > UPPERLIMIT) {
printf ("number is too high for processing\n");
exit(0);
}
if (size <= NDIGITS) {
//return the original string
strcpy (newString, string);
}
else {
//the division will give us meaasurement: B, K, M, or G
//the modulo will give us how many numbers before the .
//then add numbers after . to fill limit of length = 3
strncpy (newString, string, size%NDIGITS);
strcat (newString, ".");
//start reading from where we left off, and add as many characters as needed
//to have a max of NDIGITS (3 in our case)
strncat (newString, string+(size%NDIGITS), NDIGITS-(size%NDIGITS));
//append, B, K, M, or G
newString[strlen(newString)] = order[size/NDIGITS];
}
return newString;
}
string getPretty(string const &ugly) {
string result;
for(int i = 0; i < min(3, (int)ugly.size() - 1); i++) {
result.push_back(ugly[i]);
}
int dotPosition = (ugly.size() - 1) % 3;
if(ugly.size() <= 4)
result.push_back('B');
else if(ugly.size() <= 7)
result.push_back('K');
else if(ugly.size() <= 10)
result.push_back('M');
else if(ugly.size() <= 13)
result.push_back('G');
else
result.push_back('T');
int ct = 0;
if(dotPosition) {
result.insert(dotPosition, ".");
for(int i = result.size() - 2; i >= 0 && (result[i] == '0' || result[i] == '.'); i--) ct++;
}
result[result.size() - ct - 1] = result.back();
result.resize(result.size() - ct);
return result;
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
String input="12345B";
System.out.println(input.charAt(input.length()-1));
String numericContent=input.substring(0,input.length()-1);
int num=Integer.parseInt(numericContent);
if(num/Math.pow(10, 9)>=1)
{
System.out.println(num/Math.pow(10, 9)+"G");
}
else if(num/Math.pow(10, 6)>=1)
{
System.out.println(num/Math.pow(10, 6)+"M");
}
else if(num/Math.pow(10, 3)>=1)
{
System.out.println(num/Math.pow(10, 3)+"K");
}
else
{
System.out.println(numericContent+"B");
}
}
public static void main(String[] args)
{
// TODO Auto-generated method stub
String input="12345B";
System.out.println(input.charAt(input.length()-1));
String numericContent=input.substring(0,input.length()-1);
int num=Integer.parseInt(numericContent);
if(num/Math.pow(10, 9)>=1)
{
System.out.println(num/Math.pow(10, 9)+"G");
}
else if(num/Math.pow(10, 6)>=1)
{
System.out.println(num/Math.pow(10, 6)+"M");
}
else if(num/Math.pow(10, 3)>=1)
{
System.out.println(num/Math.pow(10, 3)+"K");
}
else
{
System.out.println(numericContent+"B");
}
}
int main() {
int T;
cin >> T;
while(T--) {
int main_num, precision;
string snum, hread, snum_t;
stringstream ss;
long int num;
cin >> snum;
snum_t= snum.substr(0, snum.size()-1);
num = std::stol(snum_t);
//cout << "num-> " << num << endl;
if(num < 1000) {
hread = snum;
} else if (num >= 1000 && num < 1000000) { //KB
main_num = (int) num / 1000;
precision = num % 1000;
ss << main_num ;
if(precision!=0) {
precision = precision / (int)pow(10,(num_digits(precision) - (3 - num_digits(main_num))));
ss << "." << precision;
}
ss << "K";
hread = ss.str();
} else if(num >1000000 && num < 1000000000) { //MB
main_num = (int) num / 1000000;
precision = num % 1000000;
ss << main_num;
if(precision != 0) {
precision = precision / (int)pow(10,(num_digits(precision) - (3 - num_digits(main_num))));
ss << "." << precision;
}
ss << "M";
hread = ss.str();
} else if(num == 1000000000) { //GB
hread = "1G";
} else {
cerr << "wrong input" << endl;
}
cout << hread << endl;
}
return 0;
}
public class HumanReadable {
public static String getOutput(String num, int sizeIndex) {
String[] size = {"B", "K", "M", "G"};
String output = num;
if(output.length()>4)
output = num.substring(0,4);
if(output.indexOf('.') == 3)
output = num.substring(0,3);
return output + size[sizeIndex];
}
public static void main(String[] args) {
String a = "341B";
String b = "12345B";
String c = "1000000000B";
int index = 0;
float quo = new Float(a.substring(0,a.length()-1)).floatValue();
while (quo > 999){
quo = quo / 1000;
index += 1;
}
System.out.println(getOutput(new Float(quo).toString(), index));
index = 0;
float quo1 = new Float(b.substring(0,b.length()-1)).floatValue();
while (quo1 > 999){
quo1 = quo1 / 1000;
index += 1;
}
System.out.println(getOutput(new Float(quo1).toString(), index));
index = 0;
float quo2 = new Float(c.substring(0,c.length()-1)).floatValue();
while (quo2 > 999){
quo2 = quo2 / 1000;
index += 1;
}
System.out.println(getOutput(new Float(quo2).toString(), index));
}
}
What do you guys think about this?
public static String prettyPrintBytes(String str) {
str = str.replace("B", "");
int scale = (int) Math.log10( Double.parseDouble(str) );
double val = Double.parseDouble(str) / Math.pow(10, scale);
String scaleStr = (scale < 3)?"B":"";
scaleStr = (scale >= 3 && scale < 6)?"K":scaleStr;
scaleStr = (scale >= 6 && scale < 9)?"M":scaleStr;
scaleStr = (scale >= 9)?"G":scaleStr;
if(val == (long) val)
return String.format("%d",(long)val) + "" + scaleStr;
else
return String.format("%.2f",val) + "" + scaleStr;
}
Sorry... forgot about a special case... Here`s the complete one.
public static String prettyPrintBytes(String str) {
str = str.replace("B", "");
int scale = (int) Math.log10(Double.parseDouble(str));
double val = Double.parseDouble(str) / Math.pow(10, scale);
String scaleStr = (scale < 3) ? "B" : "";
scaleStr = (scale >= 3 && scale < 6) ? "K" : scaleStr;
scaleStr = (scale >= 6 && scale < 9) ? "M" : scaleStr;
scaleStr = (scale >= 9) ? "G" : scaleStr;
if (scale < 3)
return str + "B";
if (val == (long) val)
return String.format("%d", (long) val) + "" + scaleStr;
else
return String.format("%.2f", val) + "" + scaleStr;
}
///public class HelloWorld{
public static void main(String []args){
String inputSize=args[0];
System.out.println("Input Size:"+inputSize);
int inputLength=inputSize.length();
System.out.println("Input Length:"+inputLength--);
if (inputLength<3)
System.out.println(inputSize);
System.exit(0);
int inputInt=Integer.parseInt(inputSize.substring(0,inputLength-1));
int diff=0;
if(inputLength>9)
{
diff=inputLength-9;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"G");
}
else if(inputLength>6)
{
diff=inputLength-6;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"M");
}
else if(inputLength>3)
{
diff=inputLength-3;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"K");
}
}
}
\\\
public class HelloWorld{
public static void main(String []args){
String inputSize=args[0];
System.out.println("Input Size:"+inputSize);
int inputLength=inputSize.length();
System.out.println("Input Length:"+inputLength--);
if (inputLength<3)
{
System.out.println(inputSize);
System.exit(0);
}
int inputInt=Integer.parseInt(inputSize.substring(0,inputLength-1));
int diff=0;
if(inputLength>9)
{
diff=inputLength-9;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"G");
}
else if(inputLength>6)
{
diff=inputLength-6;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"M");
}
else if(inputLength>3)
{
diff=inputLength-3;
System.out.println(inputSize.substring(0,diff)+"."+inputSize.substring(diff,diff+1)+"K");
}
}
}
- JSDUDE October 22, 2014