Created
September 2, 2021 23:21
-
-
Save go-cristian/7d9ec96e27e43782113ced82336e4421 to your computer and use it in GitHub Desktop.
Solution of some code interview for the @acme company
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function solve(ip, arr) { | |
let min = 0 | |
let max = ip | |
arr.forEach(tuple => { | |
const i = tuple[0] // 5 | |
const j = tuple[1] | |
if (i > min) { | |
const newMax = i - 1 | |
if (newMax < max){ | |
max = i - 1 | |
} | |
} | |
if (i <= min) { | |
const newMin = j + 1 | |
if (newMin>min) { | |
min = j + 1 | |
} | |
} | |
}) | |
if (min>ip) return -1 | |
return min | |
} | |
console.log(solve(8, [[0,0],[4,6],[2,3]]) === 1) | |
console.log(solve(8, [[5,6],[0,1],[2,3]]) === 4) | |
console.log(solve(8, [[5,6],[1,3],[7,7]]) === 0) | |
console.log(solve(8, [[5,6],[0,1],[1,3],[7,7]]) === 4) | |
console.log(solve(8, [[0,7],[0,7],[0,7],[0,7]]) === 8) | |
console.log(solve(8, [[0,8]]) === -1) | |
console.log(solve(8, [[0,3],[4,8]]) === -1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment