Sliding Items on a Circular Ring
Updated
Problem statement
You are given a string ring of length n that describes n slots placed in a circle. Slot i sits between slots i - 1 and i + 1, and slot 0 sits next to slot n - 1. Each character is one slot:
'.'is an empty slot.'a'to'z'is an item.'#'is a fixed wall.'~'is a sticky slot. It can catch at most one item.
All items slide counter-clockwise, toward lower indices: an item moves from slot i to slot i - 1, and from slot 0 to slot n - 1. An item keeps sliding one slot at a time until it stops:
- If the next slot is a wall, the item stops where it is.
- If the next slot holds an item that has already stopped, the item stops right behind it.
- If the item slides onto a sticky slot that is still empty, the sticky slot catches it and it stops there. An item can never slide past an empty sticky slot. Once a sticky slot holds an item, it blocks like any other stopped item.
Items never pass each other. Moving one item by one slot counts as one shift.
Return an array of two strings: the final ring after every item has stopped, and the total number of shifts written as a decimal number. In the final ring, each item is shown by its letter in the slot where it stopped, including a sticky slot that caught it. Walls stay '#', sticky slots that caught nothing stay '~', and every other slot is '.'.
Function
slideItems(ring: String) → String[]Examples
Example 1
ring = "#..a.b"return = ["#ab...","5"]The wall is in slot 0. Item a slides from slot 3 to slot 1, right after the wall: 2 shifts. Item b slides from slot 5 to slot 2, right behind a: 3 shifts. The total is 5.
Example 2
ring = "a..#.b"return = ["...#ba","2"]Item b slides from slot 5 to slot 4 and stops, because slot 3 is a wall: 1 shift. Item a is in slot 0, so its next slot is slot 5. It moves there and stops right behind b: 1 shift. The total is 2.
Example 3
ring = "#..~.ab"return = ["#..ab..","4"]Item a slides from slot 5 onto the empty sticky slot 3, which catches it: 2 shifts. The sticky slot is now full, so item b slides from slot 6 to slot 4 and stops right behind a: 2 shifts. Neither item reaches slots 1 and 2. The total is 4.
Constraints
1 <= ring.length <= 2000ringcontains only'.','#','~'and lowercase English letters.ringcontains at least one'#'or'~', so every item stops.
More IDFC Bank interview questions
- Rotate a Matrix by 90 DegreesONSITE INTERVIEW · Seen Nov 2026
- Employee Free TimeONSITE INTERVIEW · Seen Aug 2026
- Find All People With SecretONSITE INTERVIEW · Seen Jul 2026
- Minimum Cake CutsOA · Seen Jul 2026
- Smallest Adjacent ConcatenationOA · Seen Jul 2026
- Special Binary StringOA · Seen May 2026
- Maximum Profit from Ticket BundlesOA · Seen Aug 2025
- Find Consecutive Integer SequencesOA · Seen Aug 2025
Similar questions at other companies
- Stateful Log Transform and SearchRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- Feature Flags with Rules, Dependencies and ExposurePerplexity AI · ONSITE INTERVIEWPerplexity AI interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
- Digit-Coded Card ValidationCapital One · ONSITE INTERVIEWCapital One interview questions
- Query JSON Records with WHERE and ORDER BYGitHub · ONSITE INTERVIEW
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Generate Word Guess FeedbackAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Route Pattern MatcherGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Code Editor FoldingJane Street · ONSITE INTERVIEWJane Street interview questions