1554. XOR Query Desk

Medium3.5 points estimatedTrieDesignBit Manipulation

A help desk hands out numbered visitor badges, and the same badge number may be present several times. Implement a class XorDesk that starts empty and supports three operations, where every number is an integer with 0 <= x < 2^30.

  • add(x): one more copy of badge x joins the desk. Returns nothing.
  • remove(x): if at least one copy of x is present, remove exactly one copy and return true; otherwise change nothing and return false.
  • bestXor(x): return the largest value of x ^ y over all badges y currently on the desk (copies count once, it is only a maximum), or -1 if the desk is empty.

Each operation must run in O(30) time, and the structure uses O(30) memory per stored badge. At most 2000 calls are made in total.

Example 1

Input:
operations = ["XorDesk","add","add","bestXor","remove","bestXor"]arguments = [[],[5],[9],[3],[9],[3]]
Output:
[null,null,null,10,true,6]
Explanation:

With badges 5 and 9, 3 ^ 5 = 6 and 3 ^ 9 = 10, so the best is 10; after removing 9 only 5 remains and the best is 6.

Example 2

Input:
operations = ["XorDesk","bestXor","add","remove","remove","bestXor"]arguments = [[],[4],[4],[4],[4],[1]]
Output:
[null,-1,null,true,false,-1]
Explanation:

The empty desk gives -1; badge 4 is added and removed once, the second remove finds nothing and returns false, and the empty desk gives -1 again.

Constraints

0 ≤ x < 230

At most 2000 calls in total

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Input
Each case is an operation log. operations names the class first and then each method call; arguments holds the arguments for each, in the same order. Your answer is one list with a result per operation - null for the constructor and for methods that return nothing.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(30) per operation
Space
O(30 * number of stored badges)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…

operations names the class, then each method to call; arguments holds one list of arguments per operation, in the same order.