Skip to content

Instantly share code, notes, and snippets.

@colinlord
Created August 4, 2025 20:16
Show Gist options
  • Select an option

  • Save colinlord/9e32fc26b7c3ee2d57a2cf39fb50252c to your computer and use it in GitHub Desktop.

Select an option

Save colinlord/9e32fc26b7c3ee2d57a2cf39fb50252c to your computer and use it in GitHub Desktop.
Cassidy Interview QOTW: 8/4
// Given an array arr representing the positions of monsters
// along a straight line, and an integer d representing the
// minimum safe distance required between any two monsters,
// write a function to determine if all monsters are at least
// d units apart. If not, return the smallest distance found
// between any two monsters. If all monsters are safely spaced,
// return -1.
function minMonsterDistance(arr, d) {
arr.sort((a, b) => a - b);
let smallestDistanceFound = Infinity;
for (let i = 0; i < arr.length - 1; i++) {
const currentMonster = arr[i];
const nextMonster = arr[i + 1];
const distance = nextMonster - currentMonster;
if (distance < smallestDistanceFound) {
smallestDistanceFound = distance;
}
}
if (smallestDistanceFound < d) {
return smallestDistanceFound;
} else {
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment