Count Routes in a Divisor Graph
Updated
Problem statement
There are n nodes numbered from 1 to n. For any two nodes j and i with j < i, there is a one-way edge from j to i when j divides i (that is, i % j == 0).
A route to node i starts at node 1 and follows edges until it reaches node i. Two routes are different when their lists of nodes differ. For node 1 itself, the route that does not move counts as one route.
Return an array answer of length n, where answer[i - 1] is the number of different routes from node 1 to node i. Every count fits in a 32-bit signed integer.
Function
countRoutes(n: int) → int[]Examples
Example 1
n = 6return = [1,1,1,2,1,3]Nodes 2, 3 and 5 have only the edge from node 1, so each has one route. Node 4 is reached by 1 → 4 and 1 → 2 → 4, so it has 2 routes. Node 6 is reached by 1 → 6, 1 → 2 → 6 and 1 → 3 → 6, so it has 3 routes.
Example 2
n = 13return = [1,1,1,2,1,3,1,4,2,3,1,8,1]Node 1 counts one route, the one that stays at node 1. Node 12 has edges from 1, 2, 3, 4 and 6, whose counts are 1, 1, 1, 2 and 3, so it has 1 + 1 + 1 + 2 + 3 = 8 routes. Node 13 is prime, so its only edge comes from node 1 and it has 1 route.
Example 3
n = 8return = [1,1,1,2,1,3,1,4]Node 8 has edges from nodes 1, 2 and 4. Node 1 has 1 route, node 2 has 1 route and node 4 has 2 routes, so node 8 has 1 + 1 + 2 = 4 routes: 1 → 8, 1 → 2 → 8, 1 → 4 → 8 and 1 → 2 → 4 → 8.
Constraints
1 <= n <= 5000
More Expedia Online Assessment questions
- Balanced NumbersOA · Seen Aug 2026
- REST API: Discounted PriceOA · Seen Aug 2026
- Maximum Team SizeOA · Seen Mar 2026
- Per-Domain Request LimiterOA · Seen Oct 2025
- Count Equal-Frequency SubsequencesOA · Seen Mar 2025
- Count Increasing Stock PeriodsOA · Seen Mar 2025
- Get Minimum StepsOA · Seen Oct 2024
- Rank Songs by PopularityOA · Seen Oct 2024
- Calculate the SumOA · Seen Oct 2024
- Get Maximum EfficiencyOA · Seen Oct 2024
Similar questions at other companies
- Profitable Currency Exchange CycleOptiver · OAOptiver interview questions
- Frog Paths Without Long RunsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions
- Count Lucky TicketsThe D.E. Shaw Group · OAThe D.E. Shaw Group interview questions
- Count Routes Through Four Shop TypesOpenAI · OAOpenAI interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Connected Components with Divisor ThresholdInMobi · OAInMobi interview questions
- Jump Game with Prime-Step RuleUber · OAUber interview questions
- Count Shipment SchedulesMorgan Stanley · OAMorgan Stanley interview questions
- Unique Paths Through a GridPeopleStrong · OAPeopleStrong interview questions
Expedia interview experiences
- Expedia Intern Final Round in 2021: 28 Experiences (Part 4 of 6)
- Expedia Intern Finals Before 2020: 14 Experiences (Part 6 of 6)
- Expedia Intern Final Round in 2020: 24 Experiences (Part 5 of 6)
- Expedia Intern Final Round in 2022: 15 Experiences (Part 3 of 6)
- Expedia Intern Final Round in 2023: 17 Experiences (Part 2 of 6)
- Expedia SWE Intern Final Round: 126 Experiences (Part 1 of 6)