Drills
Five tasks. All on Playlist, all counted.
1. (*Playlist) Reverse()
Reverses the playlist in place, creating no new nodes — just swapping Prev
and Next.
How many pointer writes? Does it depend on n? How would you do the same to a
[]Item, and what would that cost?
2. (*Playlist) FindNode(title string, c *metrics.Counter) *Node
Finds a node by title.
Compare it with lesson 1's LinearFind. The complexity is the same — is the
number the same? Run both on the same library and explain the difference if
there is one.
3. (*Playlist) InsertAfterNode(n *Node, it Item)
Inserts a new item after a node you already hold.
Count the pointer writes. Then run it at n = 1,000 and n = 1,000,000. The number must not change. That is what O(1) means — verify it rather than believe it.
4. The middle in one pass
(*Playlist) Middle(c *metrics.Counter) *Node — returns the middle node
without using Len and walking the list only once.
(Hint: two pointers, one moving twice as fast.)
How many hops? Compare with the naive way — count first, then walk to Len/2.
5. Where the list genuinely wins
Construct a measurement in which Playlist beats []Item beyond argument.
The condition: no cheating by hiding the walk. If your scenario needs a node, say where the node comes from and count that cost too.
If you cannot find an honest scenario, write that down instead. It is the right answer more often than you would expect.