Paranoia
ByteCorp (name of the company is subject to co-incidence and not to mention any particular company, name or character) is a famous technological company in Byteania.
The CEO of ByteCorp doesn't trust anyone and thinks that his accountant managed to move huge amounts of money to a competitor company, MegaCorp.
He hires a computer crime investigator, and asks him to find inconsistencies in the money transfers.
Here is a sample transaction log of the company:
Feb SLR 4 M
Feb ENT 800 K
Mar SLR 4000 K
Mar ENT 800 K
Apr SLR 4010 K
Apr ENT 810 K
There are four columns:
1. Month of the transaction
2. Reason of the expense (SLR for "salary", ENT for "entertainment", OTR for "other")
3. Amount
4. M, K, or B (M for million, K for thousands, B for billion)
In the example above, April expenses show an inconsistency and should be reported.
Another example:
Jul SLR 4 M
Jul ENR 800 K
Jul OTR 1200 K
Aug SLR 4000 K
Aug ENR 800 K
Aug OTR 1190 K
Sep SLR 4000 K
Sep ENR 800 K
Sep OTR 1190 K
Here, July expenses show an inconsistency and should be reported..
As the computer investigator, write a program, which reads the transaction logs, detects inconsistent expenses and prints the exact month containing the "unusual" activities.
Video: https://youtu.be/3LjQC_21KqE
Output:


The CEO of ByteCorp doesn't trust anyone and thinks that his accountant managed to move huge amounts of money to a competitor company, MegaCorp.
He hires a computer crime investigator, and asks him to find inconsistencies in the money transfers.
Here is a sample transaction log of the company:
Feb SLR 4 M
Feb ENT 800 K
Mar SLR 4000 K
Mar ENT 800 K
Apr SLR 4010 K
Apr ENT 810 K
There are four columns:
1. Month of the transaction
2. Reason of the expense (SLR for "salary", ENT for "entertainment", OTR for "other")
3. Amount
4. M, K, or B (M for million, K for thousands, B for billion)
In the example above, April expenses show an inconsistency and should be reported.
Another example:
Jul SLR 4 M
Jul ENR 800 K
Jul OTR 1200 K
Aug SLR 4000 K
Aug ENR 800 K
Aug OTR 1190 K
Sep SLR 4000 K
Sep ENR 800 K
Sep OTR 1190 K
Here, July expenses show an inconsistency and should be reported..
As the computer investigator, write a program, which reads the transaction logs, detects inconsistent expenses and prints the exact month containing the "unusual" activities.
Video: https://youtu.be/3LjQC_21KqE
// Here how a data can be inconsistent is not mentioned
// So we will assume if any of the expenses of all the month is maximum then the data is inconsistent
const transactionLog = [
['Feb', 'SLR', 4, 'M'],
['Feb', 'ENT', 800, 'K'],
['March', 'SLR', 4000, 'K'],
['March', 'ENT', 800, 'K'],
['April', 'SLR', 4010, 'K'],
['April', 'ENT', 810, 'K']
]
/* const transactionLog = [
['Jul', 'SLR', 4, 'M'],
['Jul', 'ENR', 800, 'K'],
['Jul', 'OTR', 1200, 'K'],
['Aug', 'SLR', 4000, 'K'],
['Aug', 'ENR', 800, 'K'],
['Aug', 'OTR', 1190, 'K'],
['Sep', 'SLR', 4000, 'K'],
['Sep', 'ENR', 800, 'K'],
['Sep', 'OTR', 1190, 'K']
] */
let expenseArray = [];
findInconsistency();
function findInconsistency() {
transactionLog.forEach((log, index) => {
let monthlyExpense = {
month: '',
expense: 0
}
if (log[3] === 'M') {
log[2] = log[2] * 1000;
} else if (log[3] === 'B') {
log[2] = log[2] * 100000;
}
if (transactionLog[index + 1]) {
monthlyExpense.month = transactionLog[index][0];
monthlyExpense.expense = transactionLog[index][2];
// month is of two types
if (transactionLog[index][0].toLowerCase() === transactionLog[index+1][0].toLowerCase()) {
monthlyExpense.expense += transactionLog[index + 1][2];
// can be of three types
if (transactionLog[index+2] && transactionLog[index + 1][0].toLowerCase() === transactionLog[index+2][0].toLowerCase()) {
monthlyExpense.expense += transactionLog[index + 1][2];
}
expenseArray.push(monthlyExpense);
}
}
});
getInconsistentData();
}
// get the maximum of all the expenses
// because the inconsistency is not mentioned in the description
function getInconsistentData() {
let inconsistentLog = {
month: '',
expense: 0
}
if (expenseArray && expenseArray.length > 0) {
expenseArray.forEach(monthlyExpense => {
if (monthlyExpense.expense > inconsistentLog.expense) {
inconsistentLog.expense = monthlyExpense.expense;
inconsistentLog.month = monthlyExpense.month;
}
});
}
printInconsistentData(inconsistentLog);
}
function printInconsistentData(inconsistentLog) {
console.log(`${inconsistentLog.month} month has inconsistency.`);
console.log(inconsistentLog);
}
Output:


Comments
Post a Comment