FastPrepValidate Linked List Pointer Ordering

Validate Linked List Pointer Ordering

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Arrays encode one singly linked list: next[i] is the next node index and head its first node. Every node is visited exactly once. Auxiliary backward[i] and forward[i] are node indices or -1 for null.

Return true if every non-null backward pointer targets an earlier list position and every non-null forward pointer targets a later position.

Function

validPointerOrder(next: int[], backward: int[], forward: int[], head: int) → boolean

Examples

Example 1

next = [1,2,-1]backward = [-1,0,0]forward = [2,2,-1]head = 0return = true

All auxiliary pointers point in their required list direction.

Constraints

  • All arrays have equal length.
  • All nonnegative indices are valid.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public boolean validPointerOrder(int[] next, int[] backward, int[] forward, int head) {
  // Write your code here.
}
next[1,2,-1]
backward[-1,0,0]
forward[2,2,-1]
head0
expectedtrue
Checking account…