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
Select your webinar time
By sharing your contact details, you agree to our privacy policy.
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
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
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

Intersection Of Two Arrays Problem

Intersection Of Two Arrays Problem Statement

Given two lists of numbers, find their intersection.

Example One

{
"a": [4, 2, 2, 3, 1],
"b": [2, 2, 2, 3, 3]
}

Output:

[2, 2, 3]

Example Two

{
"a": [6, 2, 4],
"b": [1, 5, 3, 7]
}

Output:

[]

Notes

  • The order of elements in the output list does not matter.
  • The frequency of any number in the intersection must be equal to the minimum of the frequency of that number in both the given lists.

Constraints:

  • 1 <= size of the input lists <= 105
  • -106 <= any element of the input lists <= 106

Code For Intersection Of Two Arrays Solution: Hashmap

/*
Asymptotic complexity in terms of the length of list \`a\` (= \`n\`) and the length of list \`b\` (= \`m\`):
* Time: O(n + m).
* Auxiliary space: O(min(n, m)).
* Total space: O(n + m).
*/
vector<int> get_intersection_with_maintained_frequency(vector<int> &a, vector<int> &b) {

    if (a.size() > b.size()) {
        return get_intersection_with_maintained_frequency(b, a);
    }
    unordered_map<int, int> frequency_a;
    for (int i = 0; i < a.size(); i++) {
        frequency_a[a[i]]++;
    }

    vector<int> result;
    for (int i = 0; i < b.size(); i++) {
        if (frequency_a[b[i]] > 0) {
            result.push_back(b[i]);
            frequency_a[b[i]]--;
        }
    }
    return result;
}

We hope that these solutions to intersection of two arrays 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.

Intersection Of Two Arrays Problem

Intersection Of Two Arrays Problem Statement

Given two lists of numbers, find their intersection.

Example One

{
"a": [4, 2, 2, 3, 1],
"b": [2, 2, 2, 3, 3]
}

Output:

[2, 2, 3]

Example Two

{
"a": [6, 2, 4],
"b": [1, 5, 3, 7]
}

Output:

[]

Notes

  • The order of elements in the output list does not matter.
  • The frequency of any number in the intersection must be equal to the minimum of the frequency of that number in both the given lists.

Constraints:

  • 1 <= size of the input lists <= 105
  • -106 <= any element of the input lists <= 106

Code For Intersection Of Two Arrays Solution: Hashmap

/*
Asymptotic complexity in terms of the length of list \`a\` (= \`n\`) and the length of list \`b\` (= \`m\`):
* Time: O(n + m).
* Auxiliary space: O(min(n, m)).
* Total space: O(n + m).
*/
vector<int> get_intersection_with_maintained_frequency(vector<int> &a, vector<int> &b) {

    if (a.size() > b.size()) {
        return get_intersection_with_maintained_frequency(b, a);
    }
    unordered_map<int, int> frequency_a;
    for (int i = 0; i < a.size(); i++) {
        frequency_a[a[i]]++;
    }

    vector<int> result;
    for (int i = 0; i < b.size(); i++) {
        if (frequency_a[b[i]] > 0) {
            result.push_back(b[i]);
            frequency_a[b[i]]--;
        }
    }
    return result;
}

We hope that these solutions to intersection of two arrays 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.

Worried About Failing Tech Interviews?

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

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

Recommended Posts

All Posts