Amazon Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
This python code first tallies the purchases for each customer ID, then sorts the customer IDs based on total purchases. The sorted IDs are used to display the results based on (lower) 25% or (upper) 75%:
CustomerTally = { }
Purchases = [
{ 'id': 1, 'purchase': 22.10, 'e-mail': "xxx1@yyy.net" },
{ 'id': 5, 'purchase': 10.50, 'e-mail': "xxx5@yyy.net" },
{ 'id': 6, 'purchase': 11.45, 'e-mail': "xxx6@yyy.net" },
{ 'id': 1, 'purchase': 12.50, 'e-mail': "xxx1@yyy.net" },
{ 'id': 2, 'purchase': 16.60, 'e-mail': "xxx2@yyy.net" },
{ 'id': 3, 'purchase': 10.50, 'e-mail': "xxx3@yyy.net" },
{ 'id': 1, 'purchase': 21.50, 'e-mail': "xxx1@yyy.net" },
{ 'id': 3, 'purchase': 11.25, 'e-mail': "xxx3@yyy.net" },
{ 'id': 4, 'purchase': 10.50, 'e-mail': "xxx4@yyy.net" },
{ 'id': 7, 'purchase': 12.25, 'e-mail': "xxx7@yyy.net" },
{ 'id': 5, 'purchase': 22.30, 'e-mail': "xxx5@yyy.net" },
{ 'id': 1, 'purchase': 11.25, 'e-mail': "xxx1@yyy.net" },
{ 'id': 2, 'purchase': 33.50, 'e-mail': "xxx2@yyy.net" },
{ 'id': 6, 'purchase': 10.50, 'e-mail': "xxx6@yyy.net" },
{ 'id': 1, 'purchase': 12.10, 'e-mail': "xxx1@yyy.net" },
{ 'id': 4, 'purchase': 10.50, 'e-mail': "xxx4@yyy.net" }
]
def TallyPurchases(PurchaseList):
global CustomerTally
# Use CustomerTally dictionary to record purchase totals per customer ID
for purchase in PurchaseList:
if (purchase['id'] in CustomerTally):
CustomerTally[purchase['id']] += purchase['purchase']
else:
CustomerTally[purchase['id']] = purchase['purchase']
return
def SortIDs( ):
global CustomerTally
SortedIDs = [ ]
# A simple insertion sort to order the customers by spend-total
for customer in CustomerTally.keys( ):
if (SortedIDs):
# Locate this new item's place in the list
iID = 0
while ((iID < len(SortedIDs)) and (CustomerTally[SortedIDs[iID]] < CustomerTally[customer])):
iID += 1
# Insert the next item into the sorted location
SortedIDs.insert(iID, customer)
else:
SortedIDs.append(customer)
return (SortedIDs)
def run( ):
global Purchases
global CustomerTally
# Tally and sort the purchase data
TallyPurchases(Purchases)
IDList = SortIDs( )
# Now print out the customer IDs and totals, indicating a 25% or 75% spend rate
iShopper = 0
percent25 = (len(IDList) * 0.25)
percent75 = (len(IDList) * 0.75)
while (iShopper < len(IDList)):
id = IDList[iShopper]
if (iShopper <= percent25):
print "25 %s $%s" %(id, CustomerTally[id])
elif (iShopper >= percent75):
print "75 %s $%s" %(id, CustomerTally[id])
else:
print "-- %s $%s" %(id, CustomerTally[id])
iShopper += 1
return
run( )
This code first tallies the individual purchases and then sorts the IDs based on purchase total. The sorted list is used to identify the (lower) 25% and the (upper) 75%:
CustomerTally = { }
Purchases = [
{ 'id': 1, 'purchase': 22.10, 'e-mail': "xxx1@yyy.net" },
{ 'id': 5, 'purchase': 10.50, 'e-mail': "xxx5@yyy.net" },
{ 'id': 6, 'purchase': 11.45, 'e-mail': "xxx6@yyy.net" },
{ 'id': 1, 'purchase': 12.50, 'e-mail': "xxx1@yyy.net" },
{ 'id': 2, 'purchase': 16.60, 'e-mail': "xxx2@yyy.net" },
{ 'id': 3, 'purchase': 10.50, 'e-mail': "xxx3@yyy.net" },
{ 'id': 1, 'purchase': 21.50, 'e-mail': "xxx1@yyy.net" },
{ 'id': 3, 'purchase': 11.25, 'e-mail': "xxx3@yyy.net" },
{ 'id': 4, 'purchase': 10.50, 'e-mail': "xxx4@yyy.net" },
{ 'id': 7, 'purchase': 12.25, 'e-mail': "xxx7@yyy.net" },
{ 'id': 5, 'purchase': 22.30, 'e-mail': "xxx5@yyy.net" },
{ 'id': 1, 'purchase': 11.25, 'e-mail': "xxx1@yyy.net" },
{ 'id': 2, 'purchase': 33.50, 'e-mail': "xxx2@yyy.net" },
{ 'id': 6, 'purchase': 10.50, 'e-mail': "xxx6@yyy.net" },
{ 'id': 1, 'purchase': 12.10, 'e-mail': "xxx1@yyy.net" },
{ 'id': 4, 'purchase': 10.50, 'e-mail': "xxx4@yyy.net" }
]
def TallyPurchases(PurchaseList):
global CustomerTally
# Use CustomerTally dictionary to record purchase totals per customer ID
for purchase in PurchaseList:
if (purchase['id'] in CustomerTally):
CustomerTally[purchase['id']] += purchase['purchase']
else:
CustomerTally[purchase['id']] = purchase['purchase']
return
def SortIDs( ):
global CustomerTally
SortedIDs = [ ]
# A simple insertion sort to order the customers by spend-total
for customer in CustomerTally.keys( ):
if (SortedIDs):
# Locate this new item's place in the list
iID = 0
while ((iID < len(SortedIDs)) and (CustomerTally[SortedIDs[iID]] < CustomerTally[customer])):
iID += 1
# Insert the next item into the sorted location
SortedIDs.insert(iID, customer)
else:
SortedIDs.append(customer)
return (SortedIDs)
def run( ):
global Purchases
global CustomerTally
# Tally and sort the purchase data
TallyPurchases(Purchases)
IDList = SortIDs( )
# Now print out the customer IDs and totals, indicating a 25% or 75% spend rate
iShopper = 0
percent25 = (len(IDList) * 0.25)
percent75 = (len(IDList) * 0.75)
while (iShopper < len(IDList)):
id = IDList[iShopper]
if (iShopper <= percent25):
print "25 %s $%s" %(id, CustomerTally[id])
elif (iShopper >= percent75):
print "75 %s $%s" %(id, CustomerTally[id])
else:
print "-- %s $%s" %(id, CustomerTally[id])
iShopper += 1
return
run( )
1. Calculate ceil(25%) from size of the list, let this be m. m customers with highest spending receive msg1, m customers with least spending receive msg2, (n-m) customers receive msg3.
2. use quick select to find i = index of m highest customers, j = index of m lowest customers - these 2m customers receive msg's 1 and 2
3. rest of the customers from i+1 to j-1 receive msg 3
The problem can be divided in 2 parts:
1. Accumulation of customer data. For this each transaction has to be searched according to customer id. If space is no constraint; we can do that using hash-tables otherwise either black-red tree or binary heap tree can be used to build list for customer data.
2. Now you have accumulated data for each customer; we can build array of that data.
3. As codewarrior has explained we can use quick_select procedure to calculate n/4 least transactions. --> message 2
3. And then another quich_select to find m/3 th index to seperate customers needed to send message 1 and 2.
class Solution
{
public static void main(string[] argv)
{
ArrayList<CustomerPurchase> customerPurchases=new ArrayList<CustomerPurchase>();
HashMap<int, CustomerPurchase> custPurMap=new HashMap<int, CustomerPurchase>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine().trim());
for(int i=0; i<n; i++)
{
string[] words=br.readLine().trim().split(" ");
int id=Integer.parseInt(words[0]), amount=Integer.parseInt(words[1]);
if(!custPurMap.containsKey(id))
{
custPurMap[id]=new CustomerPurchase(id, amount);
}
else
{
custPurMap[id].amount+=amount;
}
}
foreach(string key in custPurMap.Keys)
{
customerPurchases.Add(custPurMap[id]);
}
customerPurchases.Sort();
int twenty5Per=(int)(customerPurchases.Count*0.25);
for(int i=0; i<twenty5Per; i++)
{
System.out.println("message 1: "+customerPurchases.get(i).id);
}
for(int i=twenty5Per; i<customerPurchases.Count-twenty5Per; i++)
{
System.out.println("message 2: "+customerPurchases.get(i).id);
}
for(int i=customerPurchases.Count-twenty5Per; i<customerPurchases.Count; i++)
{
System.out.println("message 3: "+customerPurchases.get(i).id);
}
}
}
Here is working c# program for this problem.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}
{{using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}}}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}
Below is a C# working code for this.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
public class Custmer
{
public int CustId { get; set; }
public int Amount { get; set; }
public string CustName { get; set; }
}
class Program
{
public static string Message1 = "This is for targeting 25% customer who spend most";
public static string Message2 = "This is for targeting 25% customer who spend least";
public static string Message3 = "Target the rest of the customer";
static void Main(string[] args)
{
List<Custmer> lstCust = new List<Custmer>();
lstCust.Add(new Custmer { CustId = 100, Amount = 100, CustName = "Shoeb" });
lstCust.Add(new Custmer { CustId = 101, Amount = 200, CustName = "Amin" });
lstCust.Add(new Custmer { CustId = 102, Amount = 300, CustName = "Ahad" });
lstCust.Add(new Custmer { CustId = 103, Amount = 400, CustName = "Zakir" });
lstCust.Add(new Custmer { CustId = 104, Amount = 500, CustName = "Munawer" });
lstCust.Add(new Custmer { CustId = 105, Amount = 600, CustName = "Anjum" });
lstCust.Add(new Custmer { CustId = 106, Amount = 700, CustName = "Najeeb" });
lstCust.Add(new Custmer { CustId = 107, Amount = 800, CustName = "Aslam" });
lstCust.Add(new Custmer { CustId = 108, Amount = 900, CustName = "Shakeb" });
lstCust.Add(new Custmer { CustId = 109, Amount = 1000, CustName = "Asim" });
Dictionary<int, string> lstCustResult = new Dictionary<int, string>();
int CustCount = lstCust.Count();
List<Custmer> lstCustMost = new List<Custmer>();
List<Custmer> lstCustLeast = new List<Custmer>();
List<Custmer> lstCustRemaing = new List<Custmer>();
int Max25 = (CustCount * 25) / 100;
int Min = Max25;
int Reminaing = CustCount - (Max25 + Min);
lstCustMost = lstCust.OrderByDescending(x => x.Amount).Take(Max25).ToList();
lstCustLeast = lstCust.OrderBy(x => x.Amount).Take(Max25).ToList();
DisplayListElement(lstCustMost, Message1);
DisplayListElement(lstCustLeast, Message2);
lstCustMost.AddRange(lstCustLeast);
var resultRemaiing = lstCust.Except(lstCustMost).ToList();
DisplayListElement(resultRemaiing,Message3);
Console.ReadLine();
}
public static void DisplayListElement(List<Custmer> lst, string Message)
{
foreach (Custmer Cust in lst)
{
SendMessage(Cust.CustName, Cust.Amount, Message);
}
}
public static void SendMessage(string CustName,int Amount, string Message)
{
Console.WriteLine(string.Format("CustomerName={0}, you'r amount is ={1} and your message={2}", CustName, Amount, Message));
}
}
}
import java.util.*;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
List<Customer> customers = new ArrayList<>();
List<Purchace> purchases = new ArrayList<>();
IntStream.range(1, 101).forEach(item -> {
Purchace purchace =new Purchace(new Customer(item), (int)(Math.random()*1000));
purchases.add(purchace);
});
List<PercentageDS> purc = new ArrayList<>(3);
PercentageDS ds = new PercentageDS((purchases.size()));
purc.add(ds);
int index = 0;
for(Purchace purchace:purchases) {
try {
purc.get(index).add(purchace);
} catch (RuntimeException e) {
index++;
}
}
PriorityQueue<Purchace> ddds = purc.get(0).customers;
while(!ddds.isEmpty()) {
System.out.println(ddds.poll());
}
}
}
class PercentageDS {
int capacity;
PriorityQueue<Purchace> customers;
public PercentageDS(int capacity) {
this.capacity = capacity;
this.customers = new PriorityQueue<>(capacity);
}
void add(Purchace customer) throws RuntimeException{
if(this.capacity == this.customers.size())
throw new RuntimeException("Max reached");
this.customers.add(customer);
}
}
class Customer {
private int id;
public Customer(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Customer [id=");
builder.append(id);
builder.append("]");
return builder.toString();
}
}
class Purchace implements Comparable<Purchace>{
private Customer customer;
Integer purchase;
public Purchace(Customer customer, int purchase) {
this.customer = customer;
this.purchase = purchase;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public int getPurchase() {
return purchase;
}
public void setPurchase(int purchase) {
this.purchase = purchase;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Purchace [customer=");
builder.append(customer);
builder.append(", purchase=");
builder.append(purchase);
builder.append("]");
return builder.toString();
}
@Override
public int compareTo(Purchace o) {
// TODO Auto-generated method stub
return this.purchase.compareTo(o.purchase);
}
}
Maintain two heaps(max-heap and min-heap). Total purchase for each customer will go to both the heaps. Finally pick the top 1/4 entries from both the heaps for the top and bottom purchasers.
- Pravash August 23, 2015