Question presented to candidate: "Our file tree can already expand, collapse and edit. Now let users reorganise it by dragging: drop a file or folder onto a folder to move it inside, or near the top or bottom edge of any row to place it before or after. Make sure nobody can drop a folder into itself or into one of its own subfolders, and give keyboard users a way to move things too."
Requirements:
- Store the tree normalized:
id -> { name, type, parentId, childIds }, built from nested JSON on load; manual order is kept inchildIds. - HTML5 drag-and-drop on each row:
dragstart,dragover(decide and show the drop position),drop,dragend(clean up). - Drop position from the pointer inside the row: folders use top quarter = before, bottom quarter = after, middle = inside; files use top half / bottom half.
- Reject a drop whose new parent is the dragged node or any of its descendants, both while hovering (no
preventDefault, invalid indicator) and again on drop. - Reject a name clash in the destination folder. Open the folder after an inside drop. Announce every result in a live region.
- Keyboard alternative: Ctrl+X on a row, Ctrl+V on a folder (inside) or file (after), Escape cancels; Up/Down move focus.
What a strong answer should cover:
- Why normalized state makes a move O(depth) and two small edits, compared with remove-and-reinsert on nested data.
- The descendant check as a walk up the
parentIdchain. - The
dragovercontract: callingpreventDefault()is what marks a valid drop target;dataTransferdata cannot be read duringdragover, so the dragged id is kept in app state. - Drop indicators and cleanup on
dragend(also when the drop happens outside). - An accessible alternative to dragging, and how to test drag-and-drop when the test DOM has no
DataTransfer.
Clarifying questions expected:
- "Is reordering inside a folder needed, or only moving between folders?"
- "Should a move be persisted to a server, and what if it fails?"
- "Touch support: native drag-and-drop does not work on most mobile browsers; do we need pointer events?"
Code / implementation expected: Yes. The move logic with the descendant guard is the core; working drag handlers and a keyboard alternative are expected at this level.