Register for our webinar

1 hour

Step 1

Step 2

Congratulations!

You have registered for our webinar

Oops! Something went wrong while submitting the form.

Step 1

Step 2

Confirmed

You are scheduled with Interview Kickstart.

Redirecting...

Oops! Something went wrong while submitting the form.

Head of Career Skills Development & Coaching

*Based on past data of successful IK students

Given two lists of numbers, find their intersection.

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

Output:

```
[2, 2, 3]
```

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

Output:

```
[]
```

- 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 <= 10
^{5} - -10
^{6}<= any element of the input lists <= 10^{6}

```
/*
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:

- Back-end Engineering Interview Course
- Front-end Engineering Interview Course
- Full Stack Developer Interview Course

To learn more, register for the FREE webinar.

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

Given two lists of numbers, find their intersection.

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

Output:

```
[2, 2, 3]
```

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

Output:

```
[]
```

- 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 <= 10
^{5} - -10
^{6}<= any element of the input lists <= 10^{6}

```
/*
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:

- Back-end Engineering Interview Course
- Front-end Engineering Interview Course
- Full Stack Developer Interview Course

To learn more, register for the FREE webinar.

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

Hosted By

Ryan Valles

Founder, Interview Kickstart