Photo Album Insert Order
Updated
Problem statement
You are putting photos into an album one at a time, in the order they are given. Photo i has the ID photoIds[i]. When it is added, it goes to position positions[i] of the album as the album is at that moment. Positions start at 0.
The photo that was at that position, and every photo after it, moves one place to the right. If positions[i] equals the number of photos already in the album, the new photo goes at the end.
Return the photo IDs in album order after all photos have been added.
Function
buildAlbum(positions: int[], photoIds: int[]) → int[]Examples
Example 1
positions = [0,1,1,0]photoIds = [10,20,30,40]return = [40,10,30,20]The album grows like this: [10], then [10, 20]. Photo 30 goes to position 1, pushing 20 right: [10, 30, 20]. Photo 40 goes to position 0, pushing everything right: [40, 10, 30, 20].
Example 2
positions = [0,0,0]photoIds = [7,8,9]return = [9,8,7]Every photo goes to the front, so each new photo pushes the earlier ones back. The album ends in reverse order: [9, 8, 7].
Example 3
positions = [0,0,2,1,3]photoIds = [3,6,9,12,15]return = [6,12,3,15,9]Step by step: [3], [6, 3], [6, 3, 9] (position 2 is the end), [6, 12, 3, 9], and finally [6, 12, 3, 15, 9].
Constraints
1 <= photoIds.length <= 3000positions.length == photoIds.length0 <= positions[i] <= i0 <= photoIds[i] <= 10^9- All values in
photoIdsare different.
More C3.ai interview questions
- Drop Recent Duplicate MessagesOA · Seen Oct 2026
- Maximum K-Star SumOA · Seen Oct 2026
- Largest Square Sub-Grid Within a Sum LimitOA · Seen Jan 2023
- Prefix Scores With the Running MaximumOA · Seen Jan 2023
- Tree Node With Maximum BeautyOA · Seen Nov 2022
- Count Vowel Strings With Follow RulesOA · Seen Apr 2022
- Sort by Frequency, Then ValueOA · Seen Apr 2022
- Count Distinct Binary Search TreesOA · Seen Mar 2022
- Count Palindromic Subsequences of Length KOA · Seen Mar 2022
- Fewest Turns to Open a Wheel LockPHONE SCREEN · Seen Nov 2024
Similar questions at other companies
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Book Reading EveningsGoogle · OAGoogle interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Wind-Blown Leaves Remaining in a GridAbridge · OAAbridge interview questions
- Shell Command Counts with History ReferencesTikTok · OATikTok interview questions
- Count Dropped RequestsJP Morgan Chase · OAJP Morgan Chase interview questions
- Recursively Expanded Shell Command CountsHudson River Trading · OAHudson River Trading interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- First Value Below Both NeighborsCapital One · OACapital One interview questions
- REST API: Highest International StudentsRippling · OARippling interview questions