cswork / libraryOpen practice workspace

Interview questions · Microsoft

Implement a generic tree Node class

This question evaluates skills in data structure and API design for mutable rooted trees, including node representation, parent/children management, iterator implementation (DFS/BFS), serialization/deserialization, cycle-safety, and time-complexity reasoning within the Coding & Algorithms domain.

Design and implement a Node class for a mutable rooted tree. Requirements: store value, parent, and ordered children; support add_child, remove, move_subtree, and replace_child in O(1) to O(k) time where k is affected children; provide DFS and BFS iterators; compute depth, subtree size, and path-to-root with efficient updates (e.g., maintain cached sizes and invalidate/update on mutations); serialize/deserialize to and from a nested list or JSON representation; and ensure the API is safe against creating cycles. Provide method signatures, complexity analysis, and tests demonstrating the operations.

Community answers

Nothing here yet.

Join in on CSWork →