Count Groups
Updated
Problem statement
Twilio's IT team wants to send two new USB cables to every software engineer. There are many USB standards and most of them do not work with each other, so the team labeled all n cables with a numeric tag. The tags come as a list tags, where cable number i (counting from 1) has tag tags[i]. For example, if tags = [2, 3, 1, 2], cable number 2 has tag 3.
The team wants to group the cables in pairs of the same type. The rules for grouping are:
- Two cables can be grouped together only if they have the same tag.
- A group always has exactly 2 cables.
- Each cable can be in at most one group.
A query [left, right] looks only at cables number left to right (both included). Its answer is the maximum number of groups that can be formed from those cables. For example, if tags = [2, 3, 2, 1] and the query is [1, 3], the cables in range have tags [2, 3, 2], and 1 group can be formed from the two cables with tag 2.
The queries are independent of each other. Return the answers in the same order as the queries.
Complete the function countGroups in the editor.
countGroups has the following parameters:
int tags[n]: the tags of the cablesint queries[q][2]: each query is[left, right], using cable numbers from 1 ton
The function returns an int[q] array: the number of groups that can be formed for each query.
Function
countGroups(tags: int[], queries: int[][]) → int[]Examples
Example 1
tags = [2, 3, 4, 2]queries = [[1, 4], [3, 4]]return = [1, 0]- For the first query, all cables are considered. Cables 1 and 4 can be grouped as their tags are the same.
- The tag of the cable number 1 is 2 and the tag of the cable number 4 is also 2.
- There are no other group formations possible. So, the number of groups is 1.
- For the second query, cables 3 and 4 are considered, and their tags are [4, 2].
- The tag of the cable number 3 is 4 and the tag of the cable number 4 is 2.
- Since they are different, no groups can be formed. So, the number of groups is 0.
Hence, the answer is [1, 0].
Example 2
tags = [2, 2, 2, 2]queries = [[1, 4]]return = [2]- We only have 1 query, all cables are considered.
- The tag of the cable number 1 is 2, and the tag of the cable number 2 is also 2.
- The tag of the cable number 3 is 2, and the tag of the cable number 4 is also 2.
- There are no other group formations possible. So, the number of groups is 2.
Hence, the answer is [2].
Constraints
1 ≤ n ≤ 10001 ≤ tags[i] ≤ 1091 ≤ q ≤ 1000queries[j].length = 21 ≤ left ≤ right ≤ nfor every query[left, right]
More Twilio interview questions
Similar questions at other companies
- Build the Task HierarchyPendo · OA
- Macaron Order with Set DiscountsShopify · OAShopify interview questions
- Count User Mentions in Chat MessagesRamp · OARamp interview questions
- Count Valid Square PairsAuxoAI · OA
- Two SumAmazon · OA · PHONE SCREEN
- Find Words That Can Be Formed by CharactersTabaPay · OA
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Word Frequency Counts in Alphabetical OrderAbridge · OAAbridge interview questions
- Customer Signer ScreeningState Farm · OA
- Minimum Magic Stones After MergesElevenLabs · OAElevenLabs interview questions