## Amazon Interview Question for Software Engineer / Developers

Team: TRMS
Country: United States

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

``````static void Main(string[] args)
{
const int DIVIDER = 3;
const int MAX_DIGITS = 3;
const char BYTES = 'B';
const char KILOBYTES = 'K';
const char MEGABYTES = 'M';
const char GIGABYTE = 'G';

// Get the input from the console

// Extract the bytes from the input
char lastChar = stringInput[stringInput.Length - 1];
string stringBytes = stringInput.Substring(0, stringInput.Length - 1);

// Get the length of the input bytes
int lengthBytes = stringBytes.Length;

// If the length of input bytes is less than 3 digits,
// output is same as input
// E.G. 34K
if (lengthBytes <= MAX_DIGITS)
{
Console.WriteLine("The output is: {0}", stringInput);
return;
}

// Length divided by 3 (10 to the power 3 is the multiple)
// Decides if it is KB, MB or GB
int quotient = (lengthBytes - 1)/ DIVIDER;

// Length modulo 3 decides where the decimal is placed
int modulo = lengthBytes % DIVIDER;

char unit = 'O';

switch (quotient)
{
case 0:
unit = BYTES;
break;
case 1:
unit = KILOBYTES;
break;
case 2:
unit = MEGABYTES;
break;
case 3:
unit = GIGABYTE;
break;
}

// Modulo 0 means no decimal required
// It is a multiple of 3
int max = (modulo == 0) ? MAX_DIGITS : modulo;

string result_dec = stringBytes.Substring(0, max);

if (result_dec.Length == MAX_DIGITS)
{
Console.WriteLine("The output is: {0}", string.Concat(result_dec, unit));
return;
}

// Add the decimal after the digits
string result = string.Concat(result_dec, '.', stringBytes.Substring(max , MAX_DIGITS - max), unit);
Console.WriteLine("The output is: {0}", result);

return;
}``````

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

I have not handled the trailing and ending zeros scenario in my code.

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

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" /
}

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;
}``````

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

``````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;
}``````

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

``````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");
}

}``````

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

``````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");
}

}``````

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

``````int main() {
int T;
cin >> T;
while(T--) {
int main_num, precision;
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) {
} 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";

} 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";
} else if(num == 1000000000) { //GB
} else {
cerr << "wrong input" << endl;
}
}
return 0;``````

}

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

``````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));

}
}``````

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

Using Chain of Responsibility would be an elegant OOP solution

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

``````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;

}``````

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

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;

}``````

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

``````///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");
}

}
}
\\\``````

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

``````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");
}

}
}``````

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.

### 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.