Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Array and Linked List Coding

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Array and Linked List Coding. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Atlas Copco SMARTLINK receives compressor alerts in a singly linked list. Given an integer array priority, reorder the list so that alerts whose values appear in priority are grouped first in the exact order specified by the array. Alerts with values absent from priority must follow afterward in their original relative order.

If multiple linked-list nodes have the same alert value, preserve their original relative order within that alert group. Relink the existing nodes instead of creating replacement nodes.

Formal Specification

Implement prioritize_alerts(head, priority).

  • head is the head of a singly linked list whose nodes contain an integer val and a next pointer.
  • priority is an array of unique integers.
  • Return the new linked-list head.
  • An empty list must return None.
  • In examples and test cases, linked lists are represented as arrays for serialization. For example, [4, 2, 7] represents 4 -> 2 -> 7.

Constraints

  • 0 <= head.length <= 10^5
  • 0 <= priority.length <= 10^5
  • -10^9 <= node.val <= 10^9
  • Values in priority are unique
  • The linked list may contain duplicate values

Function Signature

def prioritize_alerts(head, priority):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output