Cogwheel Rotation Directions
Updated
Problem statement
You are given nCogwheels gears numbered from 0 to nCogwheels - 1 and a list of connections. Each connection [u, v] means that gears u and v mesh with each other. All gears belong to one connected undirected graph, so every gear is reachable from gear 0.
Gear 0 is driven clockwise. Two gears that mesh directly with each other must rotate in opposite directions.
Return an array [clockwiseCount, counterclockwiseCount] containing the number of gears rotating in each direction. If the connections make it impossible to follow these rules and the mechanism locks, return [-1, -1].
Function
countRotations(nCogwheels: int, connections: int[][]) → int[]Examples
Example 1
nCogwheels = 5connections = [[0,1],[1,2],[2,3],[3,4],[0,3]]return = [3,2]Gears 0, 2, 4 rotate clockwise, and gears 1, 3 rotate counterclockwise. Every connected pair rotates oppositely, so the result is [3, 2].
Example 2
nCogwheels = 3connections = [[0,1],[1,2],[0,2]]return = [-1,-1]Gear 1 must rotate counterclockwise, so gear 2 must rotate clockwise through connection [1, 2]. But connection [0, 2] requires gear 2 to rotate counterclockwise. The contradiction locks the mechanism.
Example 3
nCogwheels = 5connections = [[0,1],[0,2],[0,3],[0,4]]return = [1,4]Gear 0 rotates clockwise. All four other gears connect directly to it and rotate counterclockwise.
Constraints
1 <= nCogwheels <= 1000.- Each connection contains two valid gear indices.
- The connections form one connected undirected graph.
- Gear
0rotates clockwise.
More OpenAI Online Assessment questions
- Count Routes Through Four Shop TypesOA · Seen Oct 2026
- Digit-Sum Sequence Join PointOA · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
- Password Clues and the Next CommandOA · Seen Oct 2026
- Longest Increasing Price SubsequenceOA · Seen Oct 2026
- Macaron Order PriceOA · Seen Oct 2026
- Shortest Atom Action PathOA · Seen Oct 2026
- Simplify a Quadratic ExpressionOA · Seen Oct 2026
- Minimum-Cost Color SequenceOA · PHONE SCREEN · Seen Oct 2026
- Largest Microorganism After ConsumptionOA · Seen Oct 2026
Similar questions at other companies
- Distances to CyclesBelvedere Trading · OA
- Shortest Path in an Unweighted GraphCitadel · OACitadel interview questions
- Crawl Reachable Pages with Bounded RetriesSnowflake · PHONE SCREENSnowflake interview questions
- Breadth-First Order of a GraphAdobe · PHONE SCREENAdobe interview questions
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
- Minimize CommuteDatabricks · OA · PHONE SCREENDatabricks interview questions
- Tree Pythagorean TriplesIBM · OAIBM interview questions
- Shortest Path from an Encoded GraphIntuit · OAIntuit interview questions
- Minimize Malware Spread by Removing a NodeVisa · OAVisa interview questions
- Longest Distance in a Frequency-Limited NetworkBloomberg LP · OABloomberg LP interview questions