FastPrepSpecial City: Count Pairs by Distance
Problems/Special City: Count Pairs by Distance

Special City: Count Pairs by Distance

Adobe logoAdobe● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Adobe online assessment for new grads. The report gives the line of cities, the extra road, the counts of unordered pairs, and an example with three cities. Its wording about the range of the road has a typo at one end. Our version uses exactly N cities. The report does not show a limit on N, so we use N up to 2000. Our version is about 97% the same as the reported question.

There are N cities numbered from 1 to N. An undirected road connects each pair i and i+1 for 1 <= i < N. One extra undirected road connects X and Y.

Return an array of length N. Entry k-1 is the number of unordered city pairs (i,j), with i < j, whose shortest path has exactly k roads. A self road or an extra copy of an existing road does not shorten a path.

Function

shortestPath(N: int, X: int, Y: int) → int[]

Examples

Example 1

N = 3X = 1Y = 3return = [3,0,0]

The extra road makes every pair adjacent: (1,2), (1,3) and (2,3). There are three pairs at distance one and none at distances two or three.

Example 2

N = 4X = 2Y = 2return = [3,2,1,0]

The self road changes no shortest path. A four-city line has three pairs one road apart, two pairs two roads apart and one pair three roads apart.

Constraints

  • For this exercise, assume 1 <= N <= 2000.
  • 1 <= X <= N and 1 <= Y <= N.

Source note: Original OA task images and any included source example. The practice contract follows the rules stated in the problem and source note.

More Adobe Online Assessment questions

  • Base Unit ConversionsOA · Seen Nov 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
  • Top-Funded Villages and ProjectsOA · Seen Aug 2026
  • Count Parent Nodes With a Target SumOA · Seen Aug 2026
  • Largest Binary Number by ConcatenationOA · Seen Aug 2026
  • Count Events by CategoryOA · Seen Aug 2026
  • Graph M-ColoringOA · Seen Aug 2026
  • Hall of Shifting TilesOA · Seen Aug 2026
  • K-th Ancestor Subtree Preorder QueryOA · Seen Aug 2026
  • Maze ReachabilityOA · Seen Aug 2026

Similar questions at other companies

  • Shortest Hop Path Between MachinesGoogle · ONSITE INTERVIEWGoogle interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
  • Cities on Shortest Delivery RoutesMathWorks · OAMathWorks interview questions
  • Optimal TransferSnowflake · OASnowflake interview questions
  • Shortest Path from an Encoded GraphIntuit · OAIntuit interview questions

Adobe interview experiences

  • Adobe New Grad OA: Unit Conversion and Code Review: 1 Experience
  • Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
  • Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
  • Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
  • Adobe New Grad MTS Interview Questions: 7 Experiences
  • Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)
All Adobe interview questionsSee Adobe hiring insights
Saved
class Solution {
    public int[] shortestPath(int N, int X, int Y) {
        // Write your solution here.
    }

}
Checking account…