DeepInterview

Layer-by-Layer Git Commit Manager

Coding2025/11

Description

Problem

Implement a program to handle layer-by-layer commit overwriting in Git.

You are given a singly linked list of commits, each with an id and a pointer to the next commit (head is the most recent).

Operations

  • Rollback(k): Remove the latest k commits, returning the list starting from the new head.
  • Package(k): Merge the latest k commits into a single commit whose id is the concatenation of those k commit ids (from newest to oldest, joined with _), followed by the remaining list.

Examples

Initial list:
commit5 -> commit4 -> commit3 -> commit2 -> commit1

Rollback(2)
Output: commit3 -> commit2 -> commit1

Package(3)
Output: commit5_commit4_commit3 -> commit2 -> commit1

Constraints

  • List length ≥ 1.
  • Rollback count ≤ list length.
  • Package count ≤ list length.
  • All operations are valid inputs.
  • Aim for O(k) for each operation and O(1) extra space.

Output

Return the modified linked list after the operation.

Notes

  • Phone screen level.
  • Keep implementation efficient and memory-conscious.
Loading editor…
OUTPUTLast run results appear here.
No output yet. Click "Run Code" to see results.