ADP Interview Question
Dev Leads Dev LeadsCountry: India
I will tell an example for suppose a user ordered $2000 worth 3 products with weights 1000g, 500g, and 500g. Now we need to split as per 1st condition so we split into 2 packages with each 1000g. Costs of the products also same 1000, 500 and 500 rupees. Check now each package doesn't have more than 1000 rupees worth products, if package has more than 1000 than you have do divide the package into some other way
typedef struct
{
int cost;
int weight;
int couriercharges; //Indicate courier charges for x weight
}item_t;
typedef struct
{
item_t *items;//List of items
int no_of_items;//no. of items in a package
int cost; // Total cost of the package
}package_t;
#define MAX_PACKAGES = 100;
typedef
{
package_t *packagelist[MAX_PACKAGES];
int no_of_package;
}packages_t;
packages_t splits_item_package(item_t item[], length)
{
int no_of_packages = 0;
int total_no_of_item_packed = 0;
packages_t *packages;
package_t *package;
packages = (packages_t*) malloc(sizeof(packages_t));
packages->no_of_packages = 0;
if (length == 0) return -1;
//Sort item in the descending order of Costofcourier/weight ratio
//and store the same in array sortedItem
// e.g. weight { 500, 200, 700, 100 }
// cost {310,310,465,310}
// cost/Weight {0.62, 1.55, 0.66, 3.1}
// SortedOrder of Items = {100,200,500,700}
for( int i =0 ;i<length ;i++)
{
packages->packagelist[i] = (package_t*) malloc(sizeof(package_t));
package = packages->packagelist[i];
package->items = (item_t*) malloc(sizeof(item_t) * length);
package->no_of_items = 0;
package->cost = 0
j = total_no_of_item_packed;
for(j ;j< length; j++)
{
if(package->cost + sortedItems[j] <= 1000)
{
index = package->no_of_items;
package->item[index].cost = sortedItems[j].cost;
package->item[index].weight = sortedItems[j].weight;
package->item[index].courierCharges =
sortedItems[j].courierCharges;
package->cost += sortedItems[j].cost;
package->no_of_items++;
total_no_of_item_packed += package->no_of_items;
no_of_packages++;
}
else
{
break;
}
}
if(j == length)
break;
}
packages->no_of_packages = no_of_packages;
return packages;
}
Yes, In the example, i have taken above where
weight { 500, 200, 700, 100 }
Price {150,300,100,800}
Couriercost {310,310,465,310}
In the above example, equal division is not possible, so the result is optimized for courier cost. In the input set which you have taken
weight { 500, 500,1000 }
Price {500,500,1000}
Couriercost {310,310,720}
The equal divison is possible. So, output of the above logic would result in equal divison of weight and optimized courier cost. Please suggest or share some input set.
package com.concurrency.pkg;
public class OrderSplitting {
public static void main(String[] args) {
// TODO Auto-generated method stub
int itemPrice[] = { 500, 200, 400, 1000, 2500, 3000 };
int itemWeight[] = { 200, 800, 300, 700, 800, 200 };
int totalPrice = 0;
for (int i = 0; i < itemPrice.length; i++) {
totalPrice = totalPrice + itemPrice[i];
}
int reminder = totalPrice % 1000;
int totalPackage = totalPrice / 1000;
if (reminder > 0) {
totalPackage += 1;
}
for (int j = 0; j < itemWeight.length; j++)
System.out.print(itemWeight[j] / totalPackage + " , ");
}
}
Could you please clarify on condition 1 & 3.
- Anonymous November 22, 2014Condition 1 says that ' If the cost of all the items in an order is more than $1000, split those items into multiple packages, otherwise one package would be enough'
Even if items distributed in a different package with each item having cost more than 1000$ than condition 3 can never be satisfied