Fast Power
Calculate the an % b where a, b and n are all 32 bit integers.
Example
For 231 % 3 = 2
For 1001000 % 1000 = 0
Challenge
O(logn)
Calculate the an % b where a, b and n are all 32 bit integers.
For 231 % 3 = 2
For 1001000 % 1000 = 0
O(logn)
A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).
How many possible unique paths are there?
Using O(1) time to check whether an integer n is a power of 2.
For n=4, return true;
For n=5, return false;
O(1) time
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)
Given N=(10000000000)2, M=(10101)2, i=2, j=6
return N=(10001010100)2
In the function, the numbers N and M will given in decimal, you should also return a decimal number.
Minimum number of operations?
You can assume that the bits j through i have enough space to fit all of M. That is, if M=10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3 and bit 2.
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume NO duplicates in the array.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
O(log(n)) time
Determine the number of bits required to flip if you want to convert integer n to integer m.
Given n = 31 (11111), m = 14 (01110), return 2.
Both n and m are 32-bit integers.
Implement int sqrt(int x).
Compute and return the square root of x.
sqrt(3) = 1
sqrt(4) = 2
sqrt(5) = 2
sqrt(10) = 3
O(log(x))
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
For [4, 5, 1, 2, 3] and target=1, return 2.
For [4, 5, 1, 2, 3] and target=0, return -1.
Write an efficient algorithm that searches for a value in an m x n matrix.
This matrix has the following properties:
Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous row.
Consider the following matrix:
1 2 3 4 5 |
|
Given target = 3, return true.
O(log(n) + log(m)) time
The code base version is an integer start from 1 to n. One day, someone committed a bad version in the code case, so it caused this version and the following versions are all failed in the unit tests. Find the first bad version.
You can call isBadVersion to help you determine which version is the first bad one. The details interface can be found in the code’s annotation part.
1 2 3 4 5 |
|
Here we are 100% sure that the 4th version is the first bad version.
Please read the annotation in code area to get the correct way to call isBadVersion in different language. For example, Java is SVNRepo.isBadVersion(v)
You should call isBadVersion as few as possible.