Highest Bid Campaign Matcher
Updated
Problem statement
Given a list of operations, store the campaign that each INSERT adds, and answer each QUERY with the id of the campaign that bids the most among the stored campaigns that match the request on ad format and on demographics. Return the answers in the order of the queries.
An insert looks like "INSERT id bid format tag1 tag2 ...". It adds a campaign with its own id, a whole-number bid, one ad format (such as video or banner) and 0 to 6 target demographic tags (such as female or age25to34). An insert has no answer.
A query looks like "QUERY format tag1 tag2 ...". It is a bid request for one ad slot of that format, shown to a user who has the 0 to 6 listed demographic tags.
A campaign matches a request when both of these hold:
- its format is the same as the request's format, and
- every one of its target tags is among the request's tags. The request may have other tags too, so a campaign with no target tags matches every request of its format.
Among the matching campaigns, the one with the highest bid wins. If several of them share that bid, the one inserted first wins. If no campaign matches, the answer is "NONE". A query sees only the campaigns inserted before it in the list.
Function
matchCampaigns(operations: String[]) → String[]Examples
Example 1
operations = ["INSERT c1 300 video female age25to34","INSERT c2 500 video male","INSERT c3 200 banner","QUERY video female age25to34 toronto","QUERY banner male","QUERY video age25to34"]return = ["c1","c3","NONE"]The first request is for video and has the tags female, age25to34 and toronto. c2 bids more, but it targets male, which the request does not have. c1 needs female and age25to34, and the request has both, so the answer is c1. The extra tag toronto does not matter. c3 targets no tags, so it matches every banner request. The last request has only age25to34: c1 also needs female and c2 needs male, so nothing matches.
Example 2
operations = ["INSERT a 100 native","QUERY native","INSERT b 100 native","INSERT c 150 native student","QUERY native","QUERY native student"]return = ["a","a","c"]The first query sees only a. At the second query, b bids the same as a, and a was inserted first, so a stays the answer. c bids more but needs the tag student. The last request has that tag, so c wins it.
Example 3
operations = ["INSERT top 900 display sports male","INSERT mid 600 display sports","INSERT low 100 display","INSERT vid 950 video sports","QUERY display sports female","QUERY display male sports","QUERY audio sports"]return = ["mid","top","NONE"]vid has the highest bid, but it is a video campaign, so it never answers a display request. For the first request, top needs male, so the next bid down, mid, wins. The second request has both of top's tags, in a different order, so top wins. No campaign has the format audio.
Constraints
1 <= operations.length <= 1000- Each operation is
"INSERT id bid format tag..."or"QUERY format tag...", with its words separated by single spaces. - At least one operation is a
QUERY. - Each
idhas1to10characters, each a lowercase letter or a digit, and no two inserts use the sameid. - Each
bidis a whole number from1to10^9, written without leading zeros. - Each
formathas1to10lowercase letters. - Each tag has
1to10characters, each a lowercase letter or a digit. An operation has at most6tags, and no tag appears twice in one operation.
More StackAdapt interview questions
- Build a Tree from Preorder and Inorder TraversalsONSITE INTERVIEW · Seen Sep 2026
- Maximum Bananas Delivered by a CamelONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Rate Limiter with a Sliding Window per ClientPHONE SCREEN · Seen Nov 2026
- Subtract Two Large Number StringsPHONE SCREEN · Seen Nov 2026
- Running Medians and PercentilesPHONE SCREEN · Seen Aug 2026
- Nested Recipe Inventory ManagerPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Implement uniq: Unique LinesVanta · ONSITE INTERVIEW
- Most-Frequent Next-Word PredictorGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Text Embedding and Vector SearchNike, Inc. · PHONE SCREEN · ONSITE INTERVIEWNike, Inc. interview questions
- Substitution Cipher Dictionary MatcherRemitly · ONSITE INTERVIEWRemitly interview questions
- Most-Frequent Next-Word PredictorBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Account Scheduler - Availability and AcquireStripe · ONSITE INTERVIEWStripe interview questions
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- In-Memory Spreadsheet ValuesRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions