This week let’s take a look at 2 questions from Adobe’s online assessment. This was from 2019 so it might have changed in 2020.
Both of these are considered “easy” but obviously that will depend on your knowledge and practice with algorithms.
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.
Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.
Explanation: If there are 4 stones in the heap, then you will never win the game;
No matter 1, 2, or 3 stones you remove, the last stone will always be
removed by your friend.
Think about: If there are 5 stones in the heap, could you figure out a way to remove the stones such that you will always be the winner?
Note: This can be solved with time complexity O(1) and space complexity O(1).
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
If you are not familiar with Hamming distance please check out the Wikipedia description.
Given two integers
y, calculate the Hamming distance.
y < 231.
Input: x = 1, y = 4Output: 2Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑The above arrows point to positions where the corresponding bits are different.
The Hamming distance between two integer numbers is the number of positions at which the corresponding bits are different.
Note: Like the example above this can also be solved with time complexity O(1) and space complexity O(1).
As always please post your JS solutions in the comments and any questions. After most of you have had a chance to give it a try, I will post my solutions. And remember, if these are difficult to solve, don’t worry keep practicing and it will get easier!