ORIGINAL CODING QUESTION
Reverse a span of a playlist chain
A learning playlist is a linked chain of lessons; each node stores a lesson code and the index of the next node. Reverse the order of the lessons at chain positions left through right (1-based positions along the chain, inclusive) and leave every other lesson where it is. Then return the checksum of the new chain: the sum over every position p of p times the lesson code at that position.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n head, then n pairs (value next) describing nodes 0 to n-1 in index order. next is the index of the following node, or -1 for the last node. Then left right. Print the checksum.
Constraints
1 ≤ n ≤ 200; values in [-1000, 1000]; following next from head visits every node exactly once and then reaches -1. 1 ≤ left ≤ right ≤ n.
Example 1
Input
5 0 1 1 2 2 3 3 4 4 5 -1 2 4
Output
51
The chain becomes 1, 4, 3, 2, 5, and 1*1 + 2*4 + 3*3 + 4*2 + 5*5 = 51.
Example 2
Input
3 2 7 -1 8 0 9 1 1 1
Output
46
The chain reads 9, 8, 7 and a one-lesson span changes nothing: 9 + 16 + 21 = 46.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Walk to the node just before position left; a placeholder node in front of head avoids a special case when left is 1.
Hint 2
Reverse the next pointers of the span one node at a time, remembering the first span node.
Hint 3
Reconnect: the node before the span points to the old last span node, and the old first span node points to whatever followed the span.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →