4.5 years of experience. US. non-traditional background, studied philosophy in college.
const sortedOccurences = (nums, target) => {
let start = 0;
let end = nums.length - 1;
let mid;
while (start <= end) {
mid = start + Math.floor((end - start) / 2);
if (nums[mid] === target) break;
if (nums[mid] > target) {
end = mid - 1;
} else {
start = mid + 1;
}
}
if (nums[mid] !== target) return 0;
let left = mid;
let right = mid;
while (nums[left] === target) {
left--;
}
while (nums[right] === target) {
right++;
}
return right - left - 1;
}I feel like my solution was as good as it gets. time complexity is O(log n + k). N being the size of the array and k being the number of occurences of the target.
To save you a click, you are given a mouse in a maze. the mouse has two methods, hasCheese() and move() . Pretty ambiguous question, move can accept anything that represents directions, I chose strings. move returns false if it can't move there and true if it was able to move in that direction accordingly. You can imagine a sample maze as H shape. Return true if the maze has cheese.
const visited = {};
const mazeHasCheese = (mouse, row, col) => {
if (mouse.hasCheese()) return true;
const up = !visited[`${row - 1}-${col}`] && mouse.moves('up');
if (up) {
mazeHasCheese(mouse, row - 1, col);
}
visited[`${row - 1}-${col}`] = 1;
const right = !visited[`${row}-${col + 1}`] && mouse.moves('right')
if (right) {
mazeHasCheese(mouse, row, col + 1);
}
visited[`${row}-${col + 1}`] = 1;
const down = !visited[`${row + 1}-${col}`] && mouse.moves('down');
if (down) {
mazeHasCheese(mouse, row + 1, col);
}
visited[`${row + 1}-${col}`] = 1
const left = !visited[`${row}-${col - 1}`] && mouse.moves('left');
if (left) {
mazeHasCheese(mouse, row, col - 1);
}
visited[`${row}-${col - 1}`] = 1;
return up || right || down || left;
};We start at (0, 0). This works cause it's all relative. I'm not 100% on this solution but it works for an H maze, don't see why it wouldn't work for any other maze. Finished with about 2 mins to spare. Spent the next few mins asking interviewer questions. Got rejection email two days later. Overall I'm pretty pissed but just going to try again after cooldown.