FastPrepFastPrep
Problem Brief

Table Editing

OA

Hi there! At the moment, FP doesn’t yet support multi-function challenges—like the ones you see on LeetCode—but we’re actively working on adding that capability. Thanks for your patience!

In this problem, you'll implement a simplified version of Table data structure.

A table consists of records (rows) with a number of defined fields (columns). Records are numbered from 1 to n and fields have string names. A specific field in a specific record is called a cell. For our purposes, we will only consider string values.

Your implementation will need to support reads, writes, and references (detailed in the next section).

Cell references

Each cell can either store a string value or reference another cell. When a cell references another cell, it inherits that cell's value. References can chain - if cell A references cell B which references cell C, then cell A will have cell C's value.

Consider this table tracking event attendees (assume Age is a string):

If we make Carol's favorite food reference Erin's favorite food, Carol's food would change to "None". If Erin's food is later changed to "Hamburger", Carol's would automatically update to "Hamburger" as well.

References can span across fields - a cell in the "Name" field could reference a cell in the "Age" field.

You do not need to support circular references.

Your task

With this in mind, implement the Table class with the following methods:

constructor(int fields: List[string])
Initialize the table with empty records. The fields array represents the names of the fields.

get_cell(record_number: int, field: string) -> string
Get the value of the cell in the field field for the record_number record. If the cell has no value, return an empty string. You can assume record_number and field are valid.

set_cell(record_number: int, field: string, value: string) -> void
Set the value of the cell in the field field for record_number to value. You can assume record_number and field are valid.

reference_cell(record_number: int, field: string, target_record: int, target_field: string) -> void
Set the cell in the field field for the record_number record to reference the cell in the field target_field for the target_record record. You can assume that both cells exist.

delete_cell(record_number: int, field: string) -> void
Delete the contents of the cell in field field for record_number. After this operation, the cell will either be referencing another cell or have a value (this method will not be called on an empty cell).

As get_cell is the only method that returns anything, your solution will be judged on the outputs of this method.

Implement the Percentiles class with the following methods:

constructor(int: Int)
Initialize the system with the guarantee that metrics will have values in the inclusive range [0, limit]

report_data(metric: string, value: Int) -> void
Report the metric metric with the value value.

percentile(int, metric: string) -> float
Return the p percentile for the metric metric. You can assume that the metric will have some data reported already.

As percentile is the only method that returns anything, your solution will be judged on the outputs of this method.

Example:
limit = 10

Then, the following calls are made, in order:

  • - report_data("x", 3)
  • - report_data("x", 8)
  • - report_data("y", 1)
  • - percentile(25, "x") Returns 2
  • - report_data("x", 4)
  • - report_data("x", 6)
  • - report_data("x", 7)
  • - percentile(50, "x") Returns 5
  • - percentile(0, "x") Returns 5

Constraints:
• 1 ≤ limit ≤ 2,147,483,647
• 1 ≤ metric.length ≤ 25
• 0 ≤ p ≤ 100
• 1 ≤ n ≤ 100,000
All input metrics will be made in the memory of Percentiles per test case.

🌸 Endless thanks to the amazing friend who kindly shared the source!

1Example 1

Input
fields = ["Name", "City"], record_number = 1, field = "Name", value = "Alice"
Output
void
Explanation

The set_cell method is called to set the value "Alice" for the "Name" field of record number 1.

2Example 2

Input
record_number = 3, field = "City", target_record = 1, target_field = "City"
Output
void
Explanation

The reference_cell method is called to set a reference from the "City" field of record number 3 to the "City" field of record number 1.

3Example 3

Input
record_number = 3, field = "City"
Output
"New York"
Explanation

The get_cell method is called to get the value of the "City" field for record number 3, which returns "New York" because it references the "City" field of record number 1.

class Table {
    // write your code here
}

class Percentiles {
    // write your code here
}
Input

fields

["Name", "City"]

record_number

1

field

"Name"

value

"Alice"

Output

"void"

Sign in to submit your solution.