Skip to main content

Restore Finishing Order - Solution & Explanation

EasyArrayHash Table5 min readAsked at: Microsoft, Google
Practice this problem

Problem Statement

You are given an integer array order of length n and an integer array friends.

  • order contains every integer from 1 to n exactly once, representing the IDs of the participants of a race in their finishing order.
  • friends contains the IDs of your friends in the race sorted in strictly increasing order. Each ID in friends is guaranteed to appear in the order array.

Return an array containing your friends' IDs in their finishing order.

 

Example 1:

Input: order = [3,1,2,5,4], friends = [1,3,4]

Output: [3,1,4]

Explanation:

The finishing order is [3, 1, 2, 5, 4]. Therefore, the finishing order of your friends is [3, 1, 4].

Example 2:

Input: order = [1,4,5,3,2], friends = [2,5]

Output: [5,2]

Explanation:

The finishing order is [1, 4, 5, 3, 2]. Therefore, the finishing order of your friends is [5, 2].

 

Constraints:

  • 1 <= n == order.length <= 100
  • order contains every integer from 1 to n exactly once
  • 1 <= friends.length <= min(8, n)
  • 1 <= friends[i] <= n
  • friends is strictly increasing

Approach Overview

Problem Overview: You are given race-related data where each participant is associated with a finishing position. The goal is to reconstruct the final finishing order of all participants. The output should list racers sorted by their finishing rank from first to last.

Approach 1: Custom Sorting with Rank Map (O(n log n) time, O(n) space)

Create a hash map that stores each participant's finishing rank for constant-time lookup. Then apply a custom sort on the list of participants where the comparator checks the stored rank in the map. During sorting, each comparison reads two ranks and orders the racers accordingly. Sorting dominates the runtime, resulting in O(n log n) time while the rank map requires O(n) extra space.

This approach works well when the input participants are not already ordered by rank. The key insight is separating data storage (hash map for ranks) from ordering logic (custom comparator). Languages like Python use sorted(..., key=...), while Java or C++ rely on custom comparator functions. The hash table ensures each rank lookup is constant time during comparisons.

Approach 2: Direct Placement Using Rank Index (O(n) time, O(n) space)

If finishing ranks are guaranteed to be unique and range from 1..n, you can skip sorting entirely. Create a result array of size n and place each participant directly at index rank - 1. Iterate through the input once, insert the participant into the correct position, and return the array. This turns the problem into a simple indexing operation.

The runtime becomes O(n) since each participant is processed exactly once, and the space cost is the output array itself. This technique is common when working with arrays where positions directly represent ranks.

Recommended for interviews: The custom sorting approach is the most natural solution because it works for any rank representation and clearly demonstrates how to combine hash table lookups with a comparator. The direct placement optimization shows deeper understanding when ranks form a contiguous range and you recognize that sorting can be avoided.

Solution

First, we build a mapping from the order array to record the finishing position of each ID. Then, we sort the friends array based on the finishing order of these IDs in the order array.

The time complexity is O(n times log n), and the space complexity is O(n), where n is the length of the order array.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Custom Sorting with Rank MapO(n log n)O(n)General case where ranks are stored separately and ordering must be reconstructed
Direct Placement Using Rank IndexO(n)O(n)When ranks are unique and fall within the range 1..n

Video Solution

3668. Restore Finishing Order (Leetcode Easy) • Programming Live with Larry • 356 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Restore Finishing Order easy or hard?
Restore Finishing Order is considered an easy problem. The challenge focuses on understanding how to rebuild ordering using ranks and applying a simple custom sort or direct array placement technique.
Restore Finishing Order Python/Java solution
Python implementations typically use sorted() with a key function that returns the stored rank for each participant. Java solutions use Collections.sort or Arrays.sort with a custom comparator referencing the rank map. Both implementations follow the same O(n log n) custom sorting strategy.
How to solve Restore Finishing Order in O(n)?
Create an output array of size n and place each participant directly at index rank - 1. Iterate through the input once and assign participants based on their rank. Since every element is processed exactly once, the algorithm runs in O(n) time with O(n) space.
What is the best approach for Restore Finishing Order?
The most common solution uses custom sorting with a hash map that stores each participant's finishing rank. Sort the participants using a comparator that compares their ranks from the map. This runs in O(n log n) time due to sorting and uses O(n) extra space for the rank map.
Is Restore Finishing Order asked at Google/Amazon/Meta?
Problems involving restoring order from ranking data commonly appear in coding interviews at large tech companies. They test familiarity with arrays, hash tables, and custom sorting—core data structure skills frequently evaluated by companies like Amazon, Google, and Meta.
What data structure is used in Restore Finishing Order?
The primary data structures are arrays and a hash table. The hash table stores the mapping between participants and their finishing rank, enabling constant-time lookups during sorting or placement.
What is the time complexity of Restore Finishing Order?
The typical custom sorting solution runs in O(n log n) time because sorting dominates the runtime. Hash map lookups during comparisons are O(1). If ranks are guaranteed to be in the range 1..n, a direct placement approach can solve it in O(n) time.

Ready to solve this problem?

Practice Restore Finishing Order with our built-in code editor and test cases.

Practice on FleetCode