Optimize Log File
Updated
Problem statement
A log file contains a series of entries represented by a permutation of length n integers. To analyze the log efficiently, n operations, indexed from 0 to n-1, are available. Each operation involves swapping two entries in the log. The goal is to select some of these n operations and apply them in any order to the log entries to produce the lexicographically smallest permutation, facilitating a more streamlined log analysis. The task is to return this lexicographically smallest permutation of entries.
Additional notes:
- Each operation can be used at most once.
- 0-based indexing is considered.
- A permutation is an array consisting of distinct integers from
1tonin arbitrary order. - The permutation
pof lengthnis lexicographically less than the permutationqof lengthnif there is an indexisuch that for alljfrom0toi-2, the conditionp[j] = q[j]is satisfied, andp[i] < q[i].
Function
optimizeLogFile(entries: int[]) → int[]Examples
Example 1
entries = [5, 4, 1, 3, 2]return = [1, 5, 2, 4, 3]Apply operation 2 to swap entries[1] and entries[2] to get entries [5, 1, 4, 3, 2].
Apply operation 1 to swap entries[0] and entries[1] to get entries [1, 5, 4, 3, 2].
Apply operation 4 to swap entries[3] and entries[4] to get entries [1, 5, 4, 2, 3].
Apply operation 3 to swap entries[2] and entries[3] to get entries [1, 5, 2, 4, 3].
Hence, the answer is [1, 5, 2, 4, 3].
Example 2
entries = [4, 3, 2, 1]return = [1, 4, 3, 2]Apply operation 3 to swap entries[2] and entries[3] to get entries = [4, 3, 1, 2].
Apply operation 2 to swap entries[1] and entries[2] to get entries= [4, 1, 3, 2].
Apply operation 1 to swap entries[0] and entries[1] to get entries = [1, 4, 3, 2].
Constraints
1 < n <= 2 * 10^51 ≤ entries[i] < n- It is guaranteed that the array entries is a permutation of length
n.
More Google interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Minimum Operations to Make a SubsequenceOA · Seen Nov 2026
- Periodic-Color Binary Forest RootsPHONE SCREEN · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Best Stock Trade with Buy and Sell DaysPHONE SCREEN · Seen Nov 2026
- Word Chain Jumps in a GridPHONE SCREEN · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Hard Disk Clustering CostConfluent · OAConfluent interview questions
- Minimum BeautyBloomberg LP · OABloomberg LP interview questions
- Maximize K-Element AND with an Increment BudgetAmazon · OA
- Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
- Split Array Largest SumAuxoAI · OA
- Delete and Freeze GameRubrik · OARubrik interview questions
- Lock CodeVisa · OAVisa interview questions
- Starlink Beam PlannerSpaceX · OA · PHONE SCREENSpaceX interview questions
- Most Segments That Sort the ArrayRippling · OARippling interview questions
- Recoverable Elements from Range-Sum MeasurementsEthos Life · OA