Introduction

Greetings, aspiring coders! Today, we're going to delve deeply into the complexities of data structures, specifically the RBTree, and explore how to handle queries efficiently. This is a common problem often encountered in numerous data science and algorithmic problems. So let's gear up to unravel the mysteries of RBTree operations and get our hands dirty with some interactive problem solving!

RBTree Operations and Time Complexity

Before delving into the task, let's understand what an RBTree is and why we would use it. An RBTree (Red-Black Tree) is a balanced binary search tree, and it ensures that the tree remains balanced, with operations that are efficient for insertion, deletion, and lookups.

The advantages of using RBTree include the following:

  1. Extracting minimum or maximum values is efficient using tree traversal.
  2. Maintaining sorted order after every insertion or deletion, similar to a SortedSet, but typically offering better performance for lookups and modifications since it's inherently balanced.

Understanding these operations can help us utilize RBTree efficiently for our problem.

Inserting and Finding Elements

Ruby's RBTree provides methods to handle elements efficiently. For example, locating the insertion point or finding bounds can be efficiently managed using the tree's structure.

Here is an example usage of RBTree:

rbtree = RBTree.new

rbtree[1] = true
rbtree[2] = true
rbtree[4] = true
rbtree[6] = true
rbtree[8] = true

bound = rbtree.lower_bound(4)
puts bound[0]  # Output: 4
Task Statement

We aim to design a Ruby function named process_queries to process a series of distinct requests or queries efficiently. The queries consist of a list of two elements — the type of operation and the operand. The tree is initially empty when we start processing the queries.

There are three types of operations we'll handle:

  • Adding an integer to the tree (operation type 0)
  • Removing an integer from the tree (operation type 1). Whenever this operation is invoked, the integer is guaranteed to exist in the tree.
  • Finding the smallest integer that is greater than or equal to a given value (operation type 2).

The function should return the current size of the tree when the operation type is 0 or 1 and the smallest integer when the operation type is 2. If such an integer does not exist, the function should return -1.

Given a list of queries:

Ruby
[
  [0, 10],
  [2, 10],
  [0, 20],
  [1, 10],
  [2, 10]
]

The function should return: [1, 10, 2, 1, 20]

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal