Skip to content

ORIGINAL CODING QUESTION

Mirrored feedback chain

Feedback scores are stored as linked nodes, each holding a score and the index of the next node. Only the order reached by following next from head matters, not the index order. Print 1 if the scores read the same from head to tail as from tail to head, otherwise print 0.

Open practice workspace

Original exercise · easy. 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. Print 1 or 0.

Constraints

1 ≤ n ≤ 200; values in [-1000, 1000]; following next from head visits every node exactly once and then reaches -1.

Example 1

Input

4 2
2 3
1 -1
1 0
2 1

Output

1

The chain reads 1, 2, 2, 1, which mirrors.

Example 2

Input

3 0
1 2
2 -1
1 1

Output

0

Index order looks mirrored, but the chain reads 1, 1, 2.

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

Copying the scores into an array works but uses linear extra space.

Hint 2

Find the middle with slow and fast pointers, then reverse the second half by rewiring next.

Hint 3

Walk from head and from the new start of the reversed half together, comparing scores until the reversed half ends.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →