Photo Album
Updated
Problem statement
An album starts empty. You are given two integer arrays, index and identity, each of length n.
Process the photos in input order. At step i, insert the photo with ID identity[i] at zero-based position index[i] in the current album. Shift every photo at that position or farther right one position to the right.
index[i] = 0inserts at the beginning.index[i] = iappends after all photos already inserted.- Photo IDs are values, not insertion positions. Repeated IDs are allowed and represent separate photo entries.
Return the final left-to-right sequence of photo IDs after all n insertions.
Function
photoAlbum(index: int[], identity: int[]) → int[]Examples
Example 1
index = [0,1,1,0]identity = [20,30,40,10]return = [10,20,40,30]The album changes from [] to [20], then [20,30], then [20,40,30], and finally [10,20,40,30].
Example 2
index = [0,0,0,0]identity = [7,7,2,9]return = [9,2,7,7]Every photo is inserted at the front, reversing the insertion order. Both entries whose ID is 7 remain in the album.
Example 3
index = [0,1,2]identity = [5,3,2]return = [5,3,2]Each photo is appended, so the final order equals the input ID order.
Constraints
1 <= n <= 2 * 10^5.index.length = identity.length = n.0 <= index[i] <= ifor every0 <= i < n.0 <= identity[i] <= 10^9. IDs need not be distinct.
Similar questions at other companies
- Rolling MetricsAkuna Capital · OAAkuna Capital interview questions
- Minimum Bridge TurnbacksBNSF Railway · OABNSF Railway interview questions
- Count Smaller Prior ElementsTata Consultancy Services · OATata Consultancy Services interview questions
- Horizontal Pod AutoscalerSnowflake · OASnowflake interview questions
- Horizontal Pod AutoscalerRubrik · OARubrik interview questions
- Adjust Prices 🍱DoorDash · OADoorDash interview questions
- Photo AlbumFactSet · OAFactSet interview questions
- Maximum Profit from an Increasing Price TripletOdoo · OAOdoo interview questions
- Count Decreasing TripletsAirbnb · OAAirbnb interview questions
- Maximize Shipment Order by Sorting One WindowAmazon · OA