Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.
It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Example 1:
Input: paths = [["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]] Output: "Sao Paulo" Explanation: Starting at "London" city you will reach "Sao Paulo" city which is the destination city. Your trip consist of: "London" -> "New York" -> "Lima" -> "Sao Paulo".
Example 2:
Input: paths = [["B","C"],["D","B"],["C","A"]] Output: "A" Explanation: All possible trips are: "D" -> "B" -> "C" -> "A". "B" -> "C" -> "A". "C" -> "A". "A". Clearly the destination city is "A".
Example 3:
Input: paths = [["A","Z"]] Output: "Z"
Constraints:
1 <= paths.length <= 100paths[i].length == 21 <= cityAi.length, cityBi.length <= 10cityAi != cityBiProblem summary: You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city. It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[["London","New York"],["New York","Lima"],["Lima","Sao Paulo"]]
[["B","C"],["D","B"],["C","A"]]
[["A","Z"]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1436: Destination City
class Solution {
public String destCity(List<List<String>> paths) {
Set<String> s = new HashSet<>();
for (var p : paths) {
s.add(p.get(0));
}
for (int i = 0;; ++i) {
var b = paths.get(i).get(1);
if (!s.contains(b)) {
return b;
}
}
}
}
// Accepted solution for LeetCode #1436: Destination City
func destCity(paths [][]string) string {
s := map[string]bool{}
for _, p := range paths {
s[p[0]] = true
}
for _, p := range paths {
if !s[p[1]] {
return p[1]
}
}
return ""
}
# Accepted solution for LeetCode #1436: Destination City
class Solution:
def destCity(self, paths: List[List[str]]) -> str:
s = {a for a, _ in paths}
return next(b for _, b in paths if b not in s)
// Accepted solution for LeetCode #1436: Destination City
use std::collections::HashSet;
impl Solution {
pub fn dest_city(paths: Vec<Vec<String>>) -> String {
let s = paths
.iter()
.map(|p| p[0].clone())
.collect::<HashSet<String>>();
paths.into_iter().find(|p| !s.contains(&p[1])).unwrap()[1].clone()
}
}
// Accepted solution for LeetCode #1436: Destination City
function destCity(paths: string[][]): string {
const s = new Set<string>(paths.map(([a, _]) => a));
return paths.find(([_, b]) => !s.has(b))![1];
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.