Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Test Color Setting Functionality

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Test Color Setting Functionality. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Figma Design applies color updates to canvas nodes in order. Implement a function that validates and applies each requested color change, preserving the previous color when an update is invalid.

An update is valid only when its node_id exists in nodes and its color is a seven-character hexadecimal string in the form #RRGGBB. Valid colors are stored in lowercase. Multiple valid updates to the same node use last-write-wins behavior. Invalid updates must not change any node and must be recorded by their zero-based position in the updates list.

Formal Specification

Implement apply_color_updates(nodes, updates), where nodes is a dictionary mapping nonempty string node IDs to valid color strings, and updates is a list of dictionaries with string fields node_id and color.

Return a dictionary with:

  • colors: the final node-to-color dictionary
  • rejected: a list of indices for invalid updates, in ascending order

Constraints

  • 1 <= len(nodes) <= 10^5
  • 0 <= len(updates) <= 10^5
  • Node IDs contain printable non-whitespace characters
  • Initial colors are valid #RRGGBB strings
  • Each update contains node_id and color fields

Function Signature

def apply_color_updates(nodes, updates):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output