Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid First Name
*Invalid Last Name
*Invalid Phone Number
By sharing your contact details, you agree to our privacy policy.
Select your webinar time
Step 1
Step 2
Congratulations!
You have registered for our webinar
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career

Lexicographical Order Problem

Lexicographical Order Problem Statement

Given a bunch of key-value pairs, for each unique key find 1) the number of values and 2) the lexicographically greatest value.

Example One

{
"arr": [
"key1 abcd",
"key2 zzz",
"key1 hello",
"key3 world",
"key1 hello"
]
}

Output:

[
"key1:3,hello",
"key2:1,zzz",
"key3:1,world"
]

Example Two

{
"arr": [
"mark zuckerberg",
"tim cook",
"mark twain"
]
}

Output:

[
"mark:2,zuckerberg",
"tim:1,cook"
]

Notes

  • Input is an array of strings, each with a key and a value separated by a space.
  • Output is an array of strings with unique keys followed by a colon, the total number of values, a comma, and the lexicographically greatest of the values associated with that key in the input.
  • Order of strings in the output does not matter.

Constraints:

  • Keys and values consist of lowercase letters and digits
  • 1 <= number of strings in input <= 104
  • 1 <= length of a key <= 256
  • 1 <= length of a value <= 650
  • Keys may repeat
  • Values may repeat

We will refer to the length of the input array arr as n.

Lexicographical Order Solution: Optimal

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.

Time Complexity

O(n * (longest key + longest value)).

As comparing two strings of length k takes O(k) time.

Auxiliary Space Used

O(n * (longest key + longest value)).

As we are maintaining hashmap.

Space Complexity

O(n * (longest key + longest value)).

Code For Lexicographical Order Solution: Optimal

    /*
    * 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.

Try yourself in the Editor

Note: Input and Output will already be taken care of.

Recommended Posts

All Posts