Created
November 27, 2022 12:41
-
-
Save narender56/be954b4fce6fda2ee83b490ed99168ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Transaction { | |
id: string; | |
bank_country_code: string; | |
amount: number; | |
} | |
const latencies: Record<string, number> = {} | |
function prioritize(transactions: Transaction[], totalTime: number = 1000): Transaction[] { | |
const result: Transaction[] = []; | |
const transactionsWithTime = transactions.map(txn => ({ | |
...txn, | |
amountPerTime: txn.amount / latencies[txn.bank_country_code], | |
time: latencies[txn.bank_country_code] | |
})).sort((txn1, txn2) => txn2.amountPerTime - txn1.amountPerTime); | |
let timeTaken = 0; | |
for (let i = 0; i < transactionsWithTime.length; i++) { | |
const transaction = transactionsWithTime[i]; | |
timeTaken += transaction.time; | |
if(timeTaken > totalTime) { | |
break; | |
}; | |
result.push({ id: transaction.id, bank_country_code: transaction.bank_country_code, amount: transaction.amount }); | |
}; | |
return result; | |
} | |
const totalTime = 1000; | |
const transactions: Transaction[] = []; | |
const subsetTransactions = prioritize(transactions, totalTime); // pass actual transactions here | |
console.log(`$${subsetTransactions.reduce((acc, curr) => acc + curr.amount, 0)} is the maximum USD transaction amount that can fit into the allotted time slots(${totalTime}ms)`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment