Skip to content

Instantly share code, notes, and snippets.

@viveknarang
Created April 13, 2020 05:01
Show Gist options
  • Save viveknarang/f2ee4efd0f80ab3f9668399498ffb270 to your computer and use it in GitHub Desktop.
Save viveknarang/f2ee4efd0f80ab3f9668399498ffb270 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;
/**
* Suppose you have a comma-separated list of merchants, the platforms they’re on,
* and the versions of the platforms they're on. You also have a list of all platforms
* we support and their latest versions. We need to write a function (listMerchantsWithIssues)
* that takes these parameters and returns a list of merchants who either:
*
* • run a platform that we don’t support
* • are at least a major version behind
* • are at least two minor versions behind
* How would you do it?
*/
public class Solution {
// A map maintaining platform names and their version numbers supported by us ...
private static Map<String, String> platforms = new HashMap<String, String>();
public static void main(String[] args) {
// first digit is major version number, second digit is minor
String[][] platforms = {
{"Magento 1", "2.3.1"},
{"Magento 2", "1.2.4.1"},
{"WooCommerce", "7.0.0"},
{"BigCommerce", "5.3.1567"},
{"Miva", "6.2.4.174"},
{"Volusion", "3.3"}
};
String merchants = String.join("\n",
"Acme,Miva,6.1.3.56",
"Babcock Enterprises, Volusion, 3.0",
"Catalyst, Magento 1, 2.2.0",
"Dharma Collective, Magento 1, 1.5.0",
"Earsnip, WooCommerce, 6.3.1",
"Funny Balloons, Responsible Ecommerce, 3.5.4",
"Great Outdoors, Magento 2, 1.1.2.0",
"Hugz, BigCommerce, 5.0.3"
);
//Reason for making this method call here: Need to load the map only once!
loadPlatforms(platforms);
// The above is just an example there could be other cases
List<String> errors = listMerchantsWithIssues(platforms, merchants);
// This line is for debugging purposes only ...
//System.out.println(errors);
if(!errors.containsAll(List.of("Babcock Enterprises",
"Dharma Collective", "Earsnip",
"Funny Balloons", "Hugz"))){
System.out.println("Incorrect result: " + errors);
}
}
// Helper function to load our supported platforms in a map for faster lookup.
private static void loadPlatforms(String[][] platformx) {
for (String[] platform : platformx) {
platforms.put(platform[0].toLowerCase(), platform[1]);
}
}
// Method to check if a merchant on the list has issues or not.
private static boolean hasIssues(String platform, String version) {
// Check if the platform exists in our platforms map...
if (platforms.containsKey(platform)) {
//Assumption: Version checking only makes sense if the platform matches ...
//Get the version number from the platforms map...
String supportedVersion = platforms.get(platform);
// Split on the dot
String[] supportedVerionDigits = supportedVersion.split("\\.");
String[] versionDigits = version.split("\\.");
/*
Assumption:
Versions numbers internally maintained in the map and version passed in the merchant list
has BOTH major and minor version number separated by a dot for EACH entry.
If needed, the logic can be improved here to handle some more cases.
If the version numbers do not have both major and minor versions
separated by a dot then return true to signal an issue.
Reason: Technically if we do not have a minor version number, especially when
major version numbers match we cannot be certain if we can really
support the merchant or not. We really need both to be certain to make a decision.
Returning true to be safe incase of lack of complete information.
*/
if (supportedVerionDigits.length < 2 || versionDigits.length < 2) {
return true;
}
// Returning true in case of a malformed version number passed ...
if (versionDigits[0].trim().length() == 0 || versionDigits[1].trim().length() == 0) {
return true;
}
// Convert to an integer for comparison.
int majorSupportedVersion = Integer.parseInt(supportedVerionDigits[0].trim());
int minorSupportedVersion = Integer.parseInt(supportedVerionDigits[1].trim());
// Convert the incoming version to integer for comparison.
int majorVersion = Integer.parseInt(versionDigits[0].trim());
int minorVersion = Integer.parseInt(versionDigits[1].trim());
// If the major version difference is more than or equal to 1 then return true.
if (majorSupportedVersion - majorVersion >= 1) {
return true;
}
// If the major version is same than check if the minor version difference is greate than equal to 2.
// Assumption: minor versions should only be compared when major versions match.
if (majorSupportedVersion == majorVersion && minorSupportedVersion - minorVersion >= 2) {
return true;
}
// Finally return false to signal that the platform and version number is supported for the merchant.
return false;
}
// If the platform is not found in the platforms map return true to signal an issue.
return true;
}
// Method to go through the list of merchants and check if any of them are not supported by us.
public static List<String> listMerchantsWithIssues(
String[][] platforms,
String merchants) {
// TODO write solution here
/*
Note:
This solution is based on some reasonable assumptions.
If some assumptions are changed, the logic will need to be modified to handle the cases ...
*/
//Return list of merchant names ...
List<String> list = new ArrayList<String>();
//Split the incoming merchant list ...
String[] merchantsList = merchants.split("\n");
//Iterate through the merchant list ...
for (String merchant : merchantsList) {
// Split the merchant entry by comma ...
String[] dt = merchant.split(",");
// Pass the platform and version to the hasIssues method for further evaluation ...
// Assumption: It is assumed that major and minor versions are separated by a dot (at least)
if (dt.length == 3 && hasIssues(dt[1].trim().toLowerCase(), dt[2].trim())) {
// If hasIssues returns true then add merchant name in the return list ...
list.add(dt[0].trim());
}
}
// Return the compiled list of merchants with issues ...
return list;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment