Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Move and Copy Constructors

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

Your question is Move and Copy Constructors. 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

In Luxoft Singapore automotive cockpit integration code, components may duplicate a mutable resource or transfer its ownership. Implement both behaviors through a command processor.

A copy operation must create an independent deep copy of a source buffer. Later mutations to either buffer must not affect the other. A move operation must transfer the source buffer's underlying list to the destination in O(1) time, then leave the source in a valid empty state. The destination name is guaranteed not to exist before the operation.

Formal Specification

Implement manage_buffers(initial, operations).

  • initial is a dictionary mapping unique string names to lists of integers.
  • operations is a list of commands:
    • ['copy', source, destination]
    • ['move', source, destination]
    • ['append', name, value]
    • ['clear', name]
  • Every referenced buffer exists when used.
  • Return a dictionary containing every buffer name created or present during execution, including moved-from sources. Values must be the final integer lists.
  • Do not use serialization or a whole-state deep copy for every command.

Constraints

  • 1 <= len(initial) <= 10^4
  • 0 <= len(operations) <= 10^5
  • 0 <= len(initial[name]) <= 10^4
  • -10^9 <= value <= 10^9
  • Destination names are unique and do not already exist
  • All operations are valid

Function Signature

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