## Ebay Interview Question for Software Engineer / Developers

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

Because the value ranges are contiguous, simply use Enum to store each range as a single value. Then use Enum.values() to iterate and compare.

e.g.,

``````public enum Range {
B1(10001, 0.04f), B2(501, 0.05f), B3...;
int value;
float tax;
public Range(int value, float tax) {...}
public int getValue() {...}
public float getTax() {...}
}

then use

for (Range r : Range.values()) {
if (sum > r.getValue()) {
sum -= r.getValue();
tax += sum * r.getTax();
sum = r.getValue();
}
}``````

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

/**
* Ebay has its own system that it uses to levy taxes on the sellers.
* The taxes are computed in a progressive manner.
* For eg. if the seller sells goods worth \$25 then for the first
* 10 dollars tax=8% and on remaining 15 dollars tax = 7%.
* So total tax= 8% of 25 + 7% of 15.

The table that they use to compute the tax is as follows

\$0 - \$10 8%
\$11 - \$50 7%
\$51 - \$500 6%
\$501 - \$10000 5%
\$10001 -\$1000000 4% and so on.

Which data structure would you use to store this table and how
would you use that data structure to code a function
float computeTaxableAmount(float amount) {}

*
*
*/
public class EbayTaxProblem {

public EbayTaxProblem()
{
populateTaxValueNode();
}

class TaxValueNode{
int value1;
int value2;
float taxValue;
}

/**
* Function to populate TaxValue Nodes
*/
public void populateTaxValueNode()
{
TaxValueNode node1 = new TaxValueNode();

/** Bracket 1 */
node1.value1 = 10;
node1.value2 = 10;
node1.taxValue = 8;

node1 = new TaxValueNode();

/** Bracket 2 */
node1.value1 = 11;
node1.value2 = 50;
node1.taxValue = 7;

node1 = new TaxValueNode();

/** Bracket 3 */
node1.value1 = 51;
node1.value2 = 500;
node1.taxValue = 6;

node1 = new TaxValueNode();

/** Bracket 4 */
node1.value1 = 501;
node1.value2 = 10000;
node1.taxValue = 5;

node1 = new TaxValueNode();

/** Bracket 5 */
node1.value1 = 10001;
node1.value2 = 1000000;
node1.taxValue = 4;

}

/**
* Method to compute the Taxable Income
*
* @param amount
* @return
*/
public float computeTaxableAmount(float amount)
{
int i=0;
float taxAmount = 0;
float diffAmount =0;
float prevHigh =0;

/*for (TaxValueNode taxValueNode : taxValueNodes) {
System.out.println(taxValueNode.taxValue);
}*/

//System.exit(0);

for (TaxValueNode taxValueNode : taxValueNodes) {

if(i==0)
{
taxAmount = amount*taxValueNode.taxValue/100;
prevHigh = taxValueNode.value2;
}
else if(amount>taxValueNode.value1 && i>0)
{
taxAmount = taxAmount +
(amount - prevHigh) * taxValueNode.taxValue/100;

prevHigh = taxValueNode.value2;
}
else
{
break;
}

i++;
}

System.out.println(taxAmount);
return taxAmount;
}

public static void main(String[] args)
{
EbayTaxProblem e1 = new EbayTaxProblem();

e1.computeTaxableAmount(150);
}

}

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

use an array of {max dollar amount, tax rate} sorted by max dollar amount.
For a given amount, do binary search of the array to find entry.

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

I will add one more column to array to make calculations faster. As I will assume this code to hit many many times.
Thrid column will have pre-computed Tax for upto last slab.

So if price is \$20 then rate is in second row & this new column in second row will have value \$.8 (10*8%) so that calculations to compute tax upto \$10 are not required to be done again & again.
We can populate this column at the process start-up.

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

We can also use a simple Hash function here

0 - 10 = Bucket_1
11 - 50 = Bucket_2

thus for a given value we could compute a hash-value.

Hence a simple HashTable(K,V) with the Bucket number gives the key (K) and the % is the Value (V)

thus by looking into the HashTable, we can get the interest percent and compute the actual value!

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

We can also use a simple Hash function here

0 - 10 = Bucket_1
11 - 50 = Bucket_2

thus for a given value we could compute a hash-value.

Hence a simple HashTable(K,V) with the Bucket number gives the key (K) and the % is the Value (V)

thus by looking into the HashTable, we can get the interest percent and compute the actual value!

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

I would use a navigablemap. Benefits?? Easy to get headmap for any given \$ amount, easily changeable at runtime, and ofcourse looks sophisticated. :D Once you have the headmap, just iterate over each key-value pair, calculate taxes, decrease high limit from given \$ amount.

If it was a design pattern, I would have used decorator pattern combined with chain of responsibility. Lots of good stuff.

I liked the use of enums but it requires code change everytime tax brackets change. Something to think about.

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

public static float[] maxArray = {10,50,500,10000,1000000};
public static float[] taxValue = {8,7,6,5,4};

public float computeTexableAmount( float amount){
int index = indArrange(maxArray, 0, maxArray.length, amount);
foat tax = taxVlaue[index]/100;
float money = amount;
float taxMoney = 0.0;
for( int i = 0; i <= index; i++){
taxMoney += money * taxVale[index]/100;
money -= maxArray[i];
}
return taxMoney;
}
public int indArrange(float[] arr, int low, int high, float m){
if( low > high){
return low;
}
int midInd = (low+high)/2;
float middle = arr[(midInd];
if( middle == m){
return midInd;
}
if(m > middle){
return indArrang(arr,midInd+1, high, m);
}else{
return indArrang(arr,low, midInd-1,m);
}
}

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

Could you explain the question again? If the tax is 8% on first \$10 then should the total tax be: 8%* 10 + 7%*15?

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

``````/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package carcup;

import java.util.Set;
import java.util.TreeMap;
import java.util.Map;
/**
*
* @author rrrajesh
*/
public class ETaxes {
TreeMap<Integer,Double> taxInfo = new TreeMap<Integer,Double>();

public void setupTaxes(){
taxInfo.put(10, 0.08);
taxInfo.put(50, 0.07);
taxInfo.put(500, 0.06);
taxInfo.put(1000, 0.05);
taxInfo.put(10000, 0.04);
}
public double calculateTaxes(int amount){
int limit;
double tax,progressiveTax = 0.0;
for(Map.Entry<Integer,Double> entry : taxInfo.entrySet()){
limit = entry.getKey();
tax = entry.getValue();
System.out.println(limit+" "+tax);
if(amount <= limit){
progressiveTax += amount*tax;
break;
}else{
amount -= limit;
progressiveTax += amount*tax;
}

}

return progressiveTax;

}
public static void main(String args[]){
ETaxes et = new ETaxes();
et.setupTaxes();
System.out.println(et.calculateTaxes(25));
}
}``````

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

Associative arrays will work with the above implementation.

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.