Given a bunch of key-value pairs, for each unique key find 1) the number of values and 2) the lexicographically greatest value.
{
"arr": [
"key1 abcd",
"key2 zzz",
"key1 hello",
"key3 world",
"key1 hello"
]
}
Output:
[
"key1:3,hello",
"key2:1,zzz",
"key3:1,world"
]
{
"arr": [
"mark zuckerberg",
"tim cook",
"mark twain"
]
}
Output:
[
"mark:2,zuckerberg",
"tim:1,cook"
]
Constraints:
We will refer to the length of the input array arr
as n
.
We provided one solution. It uses a hashmap to store the current number of values and the lexicographically greatest value for each key found so far as we iterate over the input. To process another string from the input we identify the key and value and then look for a value already present in the hashmap for this key. If one is found, we increment the number of values for this key and make sure the hashmap has the lexicographically greater value of the one it already has and one from the current input string. If no value for the current key is found in the hashmap, we simply put the current one there.
After processing all the input strings in this way, we just need to convert the hashmap data to the output format.
O(n * (longest key + longest value)).
As comparing two strings of length k
takes O(k) time.
O(n * (longest key + longest value)).
As we are maintaining hashmap.
O(n * (longest key + longest value)).
/*
* Asymptotic complexity in terms of size of \`arr\` \`n\`, length of longest key \`lk\` and length of longest value \`lv\`:
* Time: O(n * (lk + lv)).
* Auxiliary space: O(n * (lk + lv)).
* Total space: O(n * (lk + lv)).
*/
static ArrayList<String> solve(ArrayList<String> arr)
{
Map<String, Entry> map = new HashMap<>();
for (String input : arr)
{
String[] pair = input.split(" ");
String key = pair[0];
String val = pair[1];
Entry entry = map.get(key);
if (entry == null)
{
map.put(key, new Entry(val)); // The new entry has count=1.
}
else
{
entry.count++;
if (val.compareTo(entry.lexGreatest) > 0)
{
entry.lexGreatest = val;
}
}
}
ArrayList<String> results = new ArrayList<String>();
for (Map.Entry<String, Entry> e: map.entrySet())
{
results.add(e.getKey() + ":" + e.getValue().count + "," + e.getValue().lexGreatest);
}
return results;
}
static class Entry
{
int count = 1;
String lexGreatest;
Entry(String lexGreatest)
{
this.lexGreatest = lexGreatest;
}
}
We hope that these solutions to lexicographical order problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.
If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.
Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.
We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:
To learn more, register for the FREE webinar.
Given a bunch of key-value pairs, for each unique key find 1) the number of values and 2) the lexicographically greatest value.
{
"arr": [
"key1 abcd",
"key2 zzz",
"key1 hello",
"key3 world",
"key1 hello"
]
}
Output:
[
"key1:3,hello",
"key2:1,zzz",
"key3:1,world"
]
{
"arr": [
"mark zuckerberg",
"tim cook",
"mark twain"
]
}
Output:
[
"mark:2,zuckerberg",
"tim:1,cook"
]
Constraints:
We will refer to the length of the input array arr
as n
.
We provided one solution. It uses a hashmap to store the current number of values and the lexicographically greatest value for each key found so far as we iterate over the input. To process another string from the input we identify the key and value and then look for a value already present in the hashmap for this key. If one is found, we increment the number of values for this key and make sure the hashmap has the lexicographically greater value of the one it already has and one from the current input string. If no value for the current key is found in the hashmap, we simply put the current one there.
After processing all the input strings in this way, we just need to convert the hashmap data to the output format.
O(n * (longest key + longest value)).
As comparing two strings of length k
takes O(k) time.
O(n * (longest key + longest value)).
As we are maintaining hashmap.
O(n * (longest key + longest value)).
/*
* Asymptotic complexity in terms of size of \`arr\` \`n\`, length of longest key \`lk\` and length of longest value \`lv\`:
* Time: O(n * (lk + lv)).
* Auxiliary space: O(n * (lk + lv)).
* Total space: O(n * (lk + lv)).
*/
static ArrayList<String> solve(ArrayList<String> arr)
{
Map<String, Entry> map = new HashMap<>();
for (String input : arr)
{
String[] pair = input.split(" ");
String key = pair[0];
String val = pair[1];
Entry entry = map.get(key);
if (entry == null)
{
map.put(key, new Entry(val)); // The new entry has count=1.
}
else
{
entry.count++;
if (val.compareTo(entry.lexGreatest) > 0)
{
entry.lexGreatest = val;
}
}
}
ArrayList<String> results = new ArrayList<String>();
for (Map.Entry<String, Entry> e: map.entrySet())
{
results.add(e.getKey() + ":" + e.getValue().count + "," + e.getValue().lexGreatest);
}
return results;
}
static class Entry
{
int count = 1;
String lexGreatest;
Entry(String lexGreatest)
{
this.lexGreatest = lexGreatest;
}
}
We hope that these solutions to lexicographical order problem have helped you level up your coding skills. You can expect problems like these at top tech companies like Amazon and Google.
If you are preparing for a tech interview at FAANG or any other Tier-1 tech company, register for Interview Kickstart's FREE webinar to understand the best way to prepare.
Interview Kickstart offers interview preparation courses taught by FAANG+ tech leads and seasoned hiring managers. Our programs include a comprehensive curriculum, unmatched teaching methods, and career coaching to help you nail your next tech interview.
We offer 18 interview preparation courses, each tailored to a specific engineering domain or role, including the most in-demand and highest-paying domains and roles, such as:
To learn more, register for the FREE webinar.
Attend our free webinar to amp up your career and get the salary you deserve.