Amazon Interview Question for Software Engineer / Developers






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

use sprintf.

- @rams May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I hope you are joking. If you are serious, then i guess you are in the belief that the interviewer is a joker.
Give us a break

- Anonymous May 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- Orr May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- Orr May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- dv May 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Algo taking 13/11 as ex:

9 % 11 != 0
99 % 11 = 0

So multiplier = 100. 2 digits are repeating.


So number is 1.[18]

If 0 at last of denom, then move one digit to left of decimal, remove 0 from denom and repeat similarly.

- Anonymous November 11, 2011 | 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