Problem · Hash Table

Table Selection and Color Updates

Learn this problem
EasyFigma logoFigmaFULLTIMEPHONE SCREENONSITE INTERVIEW

Problem statement

You are given tables and a finite sequence of operations. Each row of tables is [tableId, initialColor].

Process these operations in order:

  • ["select", tableId] makes that table the current selection.
  • ["setColor", color] changes the selected table to that color.
  • ["getColor", tableId] reads the current color of that table.

Return the colors produced by getColor operations, in operation order. Use direct table lookup so selection, update, and query each take expected O(1) time.

Function

processTableColors(tables: String[][], operations: String[][]) → String[]

Examples

Example 1

tables = [["users","white"],["orders","gray"]]operations = [["select","users"],["setColor","blue"],["getColor","users"],["getColor","orders"]]return = ["blue","gray"]

The update changes only users. The two queries then return blue and gray.

Example 2

tables = [["a","red"],["b","green"]]operations = [["select","a"],["setColor","black"],["select","b"],["setColor","white"],["getColor","a"],["getColor","b"]]return = ["black","white"]

Changing the selection directs each update to a different table.

Example 3

tables = [["only","orange"]]operations = [["getColor","only"],["select","only"],["setColor","orange"],["getColor","only"]]return = ["orange","orange"]

A query works before any selection, and setting the existing color leaves the value unchanged.

Constraints

  • 1 <= tables.length <= 200000.
  • 1 <= operations.length <= 200000.
  • Table identifiers are unique, and every referenced table exists.
  • Every setColor occurs after a select.
  • Identifiers and colors are non-empty strings containing at most 50 characters.

More Figma problems

drafts saved locally
public String[] processTableColors(String[][] tables, String[][] operations) {
    // Write your code here.
}
tables[["users","white"],["orders","gray"]]
operations[["select","users"],["setColor","blue"],["getColor","users"],["getColor","orders"]]
expected["blue", "gray"]
checking account