Mid From PDF Coding C# Coding Interview

Flatten a linked list with next and child pointers?

Short answer: public class MultiLevelNode { public int val; public MultiLevelNode next; public MultiLevelNode child; public MultiLevelNode(int x) { val = x; next = null; child = null; } } MultiLevelNode Flatten(MultiLevelNode head) { if (head == null) return null; MultiLevelNode dummy = new MultiLevelNode(0); MultiLevelNode prev = dummy; Stack<MultiLevelNode> stack = new Stack<MultiLevelNode>(); stack.Push(head); while…

Explain a bit more

(stack.Count > 0) { var curr = stack.Pop(); prev.next = curr; curr.child = null; // remove child pointer after flattening prev = curr; if (curr.next != null) stack.Push(curr.next); if (curr.child != null) stack.Push(curr.child); } return dummy.next; } Follow on: Explanation: Use a stack to perform DFS; attach nodes and remove child pointers.

Example code

public class MultiLevelNode {
public int val;
public MultiLevelNode next;
public MultiLevelNode child;
public MultiLevelNode(int x) { val = x; next = null; child = null; } } MultiLevelNode Flatten(MultiLevelNode head) { if (head == null) return null;
MultiLevelNode dummy = new MultiLevelNode(0);
MultiLevelNode prev = dummy;
Stack<MultiLevelNode> stack = new Stack<MultiLevelNode>(); stack.Push(head); while (stack.Count > 0) { var curr = stack.Pop();
prev.next = curr;
curr.child = null; // remove child pointer after flattening
prev = curr;
if (curr.next != null) stack.Push(curr.next);
if (curr.child != null) stack.Push(curr.child);
}
return dummy.next;
} Follow on: Explanation: Use a stack to perform DFS; attach nodes and remove child pointers.

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details