Programming Problems: Advanced Algorithms Flashcards

1
Q

Setting the least significant bit to zero

A

unsigned clear_last_bit(unsigned x) {

return x & (x - 1);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Lowest bit set - retain only the lowest set bit

(10100 -> 00100)

A

unsigned lowest_set_bit(unsigned x) {

return x & ~(x-1);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Counting the number of bits in a number

A

While the number is not zero clear the least significant bit.

Clearing least significant bit: x & (x-1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Folding over

(Setting all bits after the MSB to 1)

A

x |= (x >> 1);

x |= (x >> 2);

x |= (x >> 4);

x |= (x >> 8);

x |= (x >> 16);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Isolate the highest bit

A

x = foldOver(x); // makes all bits 1 after the highest bit

result = x & ~(x>>1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Rounded log2 of an integer

A

x = x-1 // x must be unsigned int

x = foldOver(x)

x=x+1

result = 0;

while (x!=0) {

x>>=1; result++;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Next power of 2 that is larger than the input

A

result = foldOver(x) + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reverse the bits in a 32bit integer

(using O(logn) time complexity)

A

x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);

x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);

x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);

x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);

x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);

return x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Int multiplication using bitwise operators

A

int multiply(int x, int y) {

int product = 0;

while (y) {

product += x << log_x(lowest_set_bit(y));

y = clear_last_bit(y);

}

return product;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

General fibonacci expression

A

F(0)=0; F(1)=1;

F(n)=F(n-1)+F(n-2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Recursive fibonacci algorithm

A

if (n==0) return 0;

if (n==1) return 1;

return fib(n-1)+fib(n-2);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the runtime of the reecursive fibonacci algorithm?

A

Exponential function of the input.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Iterative fibonacci algorithm

A

if (n==0) return 0; if (n==1) return 1;

int cur = 0; int trailing = 1;

for (int i=0;i<n></n>

<p> int temp = cur;</p>

<p> cur = cur + trailing;</p>

<p> trailing = temp;}</p>

<p>return cur;</p>

</n>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fibonacci with tail recursion

A

int fiboTail(int n, int fib1, int fib2) {

if (1==n) return fib1;

return fibTail(n-1,fib2,fib1+fib2);

}

int fibo(int n) {

if (0==n) return 0;

return fiboTail(n,0,1);

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Closed formula for calculating the nth fibonacci element

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Optimized naive prime testing

A
  1. Check if the number is 0,1 or divisible by 2 –> return immediately
  2. Iterate from 3 to sqrt(n) by 2 and try to divide n
17
Q

Eratosthene prime algorithm

A
  1. Make a boolean array of size sqrt(n)
  2. i = 2 to sqrt(n)
    1. If i is prime
      1. if i divides n –> return true
      2. j = 2*i to sqrt(n) by i: mark j not prime
18
Q

Add arithmetic operators to make the following expression true: 3 1 3 6 = 8. You can use any parentheses you’d like.

A

((3 + 1)/(3/6)=8

19
Q

Write a function that swaps two numbers without using a temporary variable.

A

a = a^b;

b=a^b;

a=a^b;

20
Q

Find the max of a and b without using comparision operators

A

int c = a - b;

int k = (c >> 31) & 0x1;

int max = a - k * c;

return max;

21
Q

How do you know from the binary representation of a number that it is negative?

A

The MSB is set to 1 (if it is an int then the 32th bit is 0)

22
Q

How do you multiply number by -1 using bitwise operators?

(in 2’s complement representation)

A
  1. Use binary NOT operator first (invert the bits)
  2. Add 1 to the result

For example: 5 = 0000 0101 –> -5 = 1111 1010 + 1 = 1111 1011

23
Q

Algorithm to calculate the approximate square of an integer.

A

Use a binary search algorithm.

Check if the square of the middle of the current range is larger, equal or less than the original number. Alter the range accordingly or stop. Stop also when the upper range is not larger than the lower range.

SQRT for decimals is similar, but alter the stop condition, so that stop, when the range is smaller than a given delta.

24
Q

How would you look for the nearest coordinate in a plane given a list of points?

A

Using a k-d tree with 2 dimensions.

25
Q

What are the time complexities of a 2 dimensional k-d tree regarding the following operations:

building, insert a point, remove a point, range search

A
  1. building: O(n*logn)
  2. insert O(logn)
  3. remove O(logn)
  4. range query O(n^(1-1/k)+m) where m is the number of points returned