Skip to content

Instantly share code, notes, and snippets.

@KOMON
Created April 30, 2020 21:51
Show Gist options
  • Save KOMON/8d4e26852895e0fb2ce39886798167d9 to your computer and use it in GitHub Desktop.
Save KOMON/8d4e26852895e0fb2ce39886798167d9 to your computer and use it in GitHub Desktop.
Research Square exercise
<?php
///////////////////////////////////////////////////////////////////////////////
//
// RESEARCH SQUARE CODING EXERCISE
//
// In this exercise, we would like you to write PHP code capable of solving the
// problems below. To begin, please make a local copy of this file and open it
// using your favorite code editor.
//
// We have designed this exercise to mimic the types of problems we solve on a
// daily basis. It is intended for engineers who are proficient in PHP and we
// expect it will take approximately 30 minutes to complete. The code you write
// will be used solely for evaluation purposes.
//
// During the exercise, you may look for help on Google, php.net, and other
// publicly available resources. However, please do *NOT* ask your friends or
// colleagues for help. We want to see *your* solution.
//
// When you are finished, please paste your solution in the questionnaire form
// OR provide a link to your solution using a tool like GitHub Gist. We will
// run your code on the command line using PHP 7.4.
//
// Your solution will be evaluated using the following criteria:
// - Does it output the correct results?
// - Are there any potential bugs or defects?
// - Is the code pragmatic and easy to understand?
// - Does it continue to work when new inputs are provided?
//
// Good luck!
//
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
// TASK #1
//
// Summary:
//
// The data file below contains a list of customers and their orders. Use
// these data to find and print the price of the most expensive order.
//
// Hint: Open the data file and look at its contents. The `total_price` field
// holds the price of an order.
//
// Expected output:
//
// Most expensive order = 500.00
//
///////////////////////////////////////////////////////////////////////////////
$url = 'https://rs-coding-exercise.s3.amazonaws.com/2020/orders-2020-02-10.json';
$response = json_decode(file_get_contents($url), true);
function mostExpensiveOrder(array $orders): string
{
// sort desc on total_price
usort($orders, fn(array $a, array $b) => $b['total_price'] <=> $a['total_price']);
$highestOrderTotal = $orders[0]['total_price'] ?? 0.0;
return 'Most expensive order = ' . number_format($highestOrderTotal, 2);
}
echo mostExpensiveOrder($response['orders']) . PHP_EOL;
///////////////////////////////////////////////////////////////////////////////
//
// TASK #2
//
// Summary:
//
// Using this same data file, calculate and print the sum of prices for all
// orders created in the previous three years, grouped by year.
//
// Expected output:
//
// Total price of orders in 2018 = 275.00
// Total price of orders in 2019 = 860.00
// Total price of orders in 2020 = 20.00
//
///////////////////////////////////////////////////////////////////////////////
function aggregateOrdersByYear(array $orders): array
{
$summary = [];
foreach ($orders as $order) {
if (!isset($order['created_date'])) {
continue;
}
$year = (new \DateTime($order['created_date']))->format('Y');
$summary[$year] = $order['total_price'] + ($summary[$year] ?? 0);
}
return $summary;
}
function reportOrderTotalsByYear(array $reportingYears, array $orders): string
{
$aggregated = aggregateOrdersByYear($orders);
$report = '';
foreach ($reportingYears as $year) {
$total = $aggregated[$year] ?? 0.0;
$report .= "Total price of orders in {$year} = " . number_format($total, 2) . PHP_EOL;
}
return $report;
}
echo reportOrderTotalsByYear(['2018', '2019', '2020'], $response['orders']);
///////////////////////////////////////////////////////////////////////////////
//
// TASK #3
//
// Summary:
//
// Using the same data file, find and print the ID and name of the customer
// with the most orders.
//
// Expected output:
//
// Customer with the most orders = [CUST-0001] Yoda
//
///////////////////////////////////////////////////////////////////////////////
function countOrdersByCustomer(array $orders = []): array
{
$summary = [];
foreach ($orders as $order) {
if (!isset($order['customer_id'])) {
continue;
}
$summary[$order['customer_id']] = 1 + ($summary[$order['customer_id']] ?? 0);
}
return $summary;
}
function customerWithMostOrders(array $orders, array $customers): string
{
$countByCustomer = countOrdersByCustomer($orders);
// sort by count desc
arsort($countByCustomer);
$mostOrdersCustomerId = array_key_first($countByCustomer) ?? '';
// reshape customers into [id => name] for easier lookup
$customerNamesById = array_column($customers, 'name', 'id');
$mostOrdersCustomerName = $customerNamesById[$mostOrdersCustomerId] ?? '<No Name Found>';
return "Customer with the most orders = [{$mostOrdersCustomerId}] {$mostOrdersCustomerName}";
}
echo customerWithMostOrders($response['orders'], $response['customers']) . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment