Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close

Maximum Gap Problem

Maximum Gap Problem Statement

Given an array of n non-negative integers, find the maximum difference between 2 consecutive numbers in the sorted array of the given integers.

Example One

{
"arr": [1, 3, 7, 2]
}

Output:

4

We can see that in sorted order, the array becomes [1, 2, 3, 7], and the maximum difference is 7 - 3 = 4.

Example Two

{
"arr": [1, 1, 1]
}

Output:

0

Here the sorted array is [1, 1, 1], and so the maximum difference is 1 - 1 = 0.

Notes

  • We need a linear time solution, not n * log(n). You can use up-to linear extra space.
  • Assume input is always valid, and won't overflow.
  • If the input has only one element, then return 0.
  • There can be duplicates elements.

Constraints:

  • 1 <= n <= 106
  • 0 <= array element <= 106

We have provided one solution for this problem.

Maximum Gap Solution: Optimal

  • First we find the minimum and maximum numbers; let's name them, min and max respectively.
  • Now the range of the numbers is maxmin + 1.
  • We create an array named buckets of this size and use it to keep frequency of occurrence of numbers in an array.
  • When we encounter the number present in the array, we increment buckets[arr[i] – max] by 1.
  • After all the numbers in the array are processed, we find the maximum number of consecutive 0's between 2 non-zero elements of buckets array. That is the answer.

Time Complexity

O(n + range).

Auxiliary Space Used

O(range).

Space Complexity

O(n + range).

Input takes O(n) space, and we use O(range) of auxiliary space.

Code For Maximum Gap Solution: Optimal


    /*
    Asymptotic complexity in terms of \`n\` = length of the given array \`arr\` and
    \`range\` = max(Elements of array) – min(Elements of array):
    * Time: O(n + range).
    * Auxiliary space: O(range).
    * Total space: O(n + range).
    */

    static Integer maximum_gap(ArrayList<Integer> arr) {
        int n = arr.size();
        if(n==1) {
            return 0;
        }

        int max = 0;
        int min = 1000001;
        // Find maximum and minimum elements.
        for(int i = 0; i<n; i++) {
            min = Math.min(min,arr.get(i));
            max = Math.max(max,arr.get(i));
        }

        // Create array of size range to store the frequencies of the array elements.
        int range = max - min + 1;
        int buckets[] = new int[range + 5];
        for(int i : arr) {
            buckets[i - min]++;
        }

        // Finding maximum number of consecutive zeros between 2 non zero elements of buckets array.
        int ans = 0;
        for(int i = range-1; i>0;) {
            int cnt = 0, temp = i;
            i--;
            while(i>=0 && buckets[i]==0) {
                cnt++;
                i--;
            }
            if(i>=0) {
                ans = Math.max(ans, temp - i);
            }
        }
        return ans;
    }

We hope that these solutions to maximum gap 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.

Maximum Gap Problem

Maximum Gap Problem Statement

Given an array of n non-negative integers, find the maximum difference between 2 consecutive numbers in the sorted array of the given integers.

Example One

{
"arr": [1, 3, 7, 2]
}

Output:

4

We can see that in sorted order, the array becomes [1, 2, 3, 7], and the maximum difference is 7 - 3 = 4.

Example Two

{
"arr": [1, 1, 1]
}

Output:

0

Here the sorted array is [1, 1, 1], and so the maximum difference is 1 - 1 = 0.

Notes

  • We need a linear time solution, not n * log(n). You can use up-to linear extra space.
  • Assume input is always valid, and won't overflow.
  • If the input has only one element, then return 0.
  • There can be duplicates elements.

Constraints:

  • 1 <= n <= 106
  • 0 <= array element <= 106

We have provided one solution for this problem.

Maximum Gap Solution: Optimal

  • First we find the minimum and maximum numbers; let's name them, min and max respectively.
  • Now the range of the numbers is maxmin + 1.
  • We create an array named buckets of this size and use it to keep frequency of occurrence of numbers in an array.
  • When we encounter the number present in the array, we increment buckets[arr[i] – max] by 1.
  • After all the numbers in the array are processed, we find the maximum number of consecutive 0's between 2 non-zero elements of buckets array. That is the answer.

Time Complexity

O(n + range).

Auxiliary Space Used

O(range).

Space Complexity

O(n + range).

Input takes O(n) space, and we use O(range) of auxiliary space.

Code For Maximum Gap Solution: Optimal


    /*
    Asymptotic complexity in terms of \`n\` = length of the given array \`arr\` and
    \`range\` = max(Elements of array) – min(Elements of array):
    * Time: O(n + range).
    * Auxiliary space: O(range).
    * Total space: O(n + range).
    */

    static Integer maximum_gap(ArrayList<Integer> arr) {
        int n = arr.size();
        if(n==1) {
            return 0;
        }

        int max = 0;
        int min = 1000001;
        // Find maximum and minimum elements.
        for(int i = 0; i<n; i++) {
            min = Math.min(min,arr.get(i));
            max = Math.max(max,arr.get(i));
        }

        // Create array of size range to store the frequencies of the array elements.
        int range = max - min + 1;
        int buckets[] = new int[range + 5];
        for(int i : arr) {
            buckets[i - min]++;
        }

        // Finding maximum number of consecutive zeros between 2 non zero elements of buckets array.
        int ans = 0;
        for(int i = range-1; i>0;) {
            int cnt = 0, temp = i;
            i--;
            while(i>=0 && buckets[i]==0) {
                cnt++;
                i--;
            }
            if(i>=0) {
                ans = Math.max(ans, temp - i);
            }
        }
        return ans;
    }

We hope that these solutions to maximum gap 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.

Worried About Failing Tech Interviews?

Attend our free webinar to amp up your career and get the salary you deserve.

Ryan-image
Hosted By
Ryan Valles
Founder, Interview Kickstart
blue tick
Accelerate your Interview prep with Tier-1 tech instructors
blue tick
360° courses that have helped 14,000+ tech professionals
blue tick
100% money-back guarantee*
Register for Webinar
All Blog Posts