Tech Interview Discuss

来源:互联网 发布:mpu6050与单片机连接 编辑:程序博客网 时间:2024/05/08 00:10

1. largest contiguous sub-array

Given an N-by-N array of black (1) and white (0) pixels, find the largest contiguous sub-array that consists of entirely black pixels. In the example below there is a 6-by-2 sub-array.

1 0 1 1 1 0 0 0
0 0 0 1 0 1 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 0 1 0
0 0 1 1 1 1 1 1
0 1 0 1 1 1 1 0
0 1 0 1 1 1 1 0
0 0 0 1 1 1 1 0

2repositioning

you are given an array of integers containing only 0s and 1s. you have to place all the 0s in even position and 1s in odd position and if suppose no if 0s exceed no. of 1s or vice versa then keep them untouched. Do that in ONE PASS and WITHOUT taking EXTRA MEMORY.

input array:
{0 1 1 0 1 0 1 0 1 1 1 0 0 1 0 1 1 }
output array:
{0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 1 }

Implement it using two pointer. First pointer points to the 0 in odd position and second one points to the 1 in even position. In the loop, swap the items in the positions where the pointers pointed to and find the next suitable positions for pointers.

3. Binary numbers

Given a binary number, how quickly can you find the next higher number with same number of ones ? How can you find the immediate lower number with same number of ones ?

--> the immediate lower number with same number of ones
* if all digits are '1', it does not exist;
* at least one digit is '0' :
  swap the leftmost '0' with the '1' at its left and
  rearrange the digits at its right '1's first and '0's then.

--> the next higher number with same number of ones
* swap the rightmost '1' with the '0' at its left and
  rearrange the digits at its right '0's first and '1's then.
(if all digits are '1', the '0' at its left is in front of the number, not written)

4. Microsoft question

Given a BST ,write a fun which will modify the given tree such that it will result in a sorted double linked list; without creating a new list. As in left becomes prev and right becomes next
 
Modify the nodes from leaves to root. The "left" points to the biggest node(travel down through the "right" pointers) in the left subtree and the "right" points to the smallest node in the right subtree.
 

5. Binary Format

(lifted as it is)
Given a number, how would you check to see if it can be accurately represented in binary (eg can 76.2782783 be represented accurately in binary
 
If a number n can be accurately represented in binary, it can be written as n=m/2^k where m and k are integers.  As m=n*2^k=n*10^k/5^k, we can treat the number removed the decimal point from n as m. And if m has factor 5^k, the n can be accurately represented in binary.
 

6. Reverse Linked List

Use recursion and don't use recursion
Use recursion:
public Item reverseLinkedList(Item item)
{
  
if(item.next!=null)
  
{
    Item tmp
=reverseLinkedList(item.next);
    item.next.next
=item;
    item.next
=null;
    
return tmp;
  }

  
return item;
}
Don't use recursion:
public Item reverseLikedListWithoutRecursion(Item item)
{
  Item previous
=null;
  
while(item!=null)
  
{
    Item tmp
=item.next;
    item
->next=previous;
    previous
=item;
    item
=tmp;
  }

  
return item;
}

 

7. Codejam question

(picked up verbatim)
Alex has a lot of contacts in his Hyper Instant Messenger (HIM), and therefore, it takes some time to find the right contact. HIM supports Contact Groups, and each contact can be placed into a group. However, HIM's group support is rather limited. It allows for only one of the following two scenarios: 1) Multiple groups, but every contact belongs to a single group, or 2) All contacts exist only in a global contacts list (and no groups are created).
It takes Alex k seconds to find an entry in any list of k entries. If groups exist, HIM will first show a list of all the groups, and once a group is selected, a list of all contacts within that group will be displayed. If groups do not exist, HIM will simply show a single list containing all the contacts in the global list. For example, if Alex has 5 contacts in the global list, it will take 5 seconds to find any one of them. If Alex splits those contacts into two groups with 2 and 3 users each, it will first take him 2 seconds to find the right group, and then 2 or 3 seconds to find the right contact depending on which group he chooses. You are given an int N representing the total number of contacts. Arrange the contacts optimally (either in one global contacts list or in several groups) such that the maximal time needed to find a contact is minimized. Return this time and the groups formed.
 
If there is k groups and n/k members in each group, the searching time is k+n/k. So if n is a square number, the searching time reaches minimum when k=sqrt(n). Otherwise, we can split the contacts into [k]+1 groups and place the contacts into these groups as evenly as possible.
 

8. Finding least common ancestor of two nodes

public Node findLCA(node a, node b, node node)
{
  
if(node==null)
    
return null;
  
if(node.left==|| node.right==|| node.left==|| node.right==b)
    
return node;
  Node p
=findLCA(a, b, node->left);
  Node q
=findLCA(a, b, node->right);
  
if(p!=null && q!=null)
    
return node;
  
else return (p!=null)?p:q;
}

It is not important which node is returned. It is important that the return value is whether null or not.

9. Google Q

I am not sure if this is the right version of the question. But here it is.
We are given an array a1...an and we need to construct b1...bn where bi = a1*a2*...*an/ai.
We are allowed only constant space and the time complexity is O(n). No divisions are allowed.

10. Binary search on circular array.

How can we search a number in O(lgn) in a circularly sorted array?
Original array-1,5,7,11,16,20
Circularly sorted array-16,20,1,5,7,11.

We don't know the position of the pivot around which the array has been rotataed.

 

11. array split

Given an array wtih length greater than 2.
U need to split the array into 2 such that the average of the two arrays must b same.
test case:
{2,3,4} is the input

xpectd o/p: {3} {2,4}
average is 3 in both the cases.
Note: If there is no solution then the system shud halt displayin "IMPOS".

 

12. Microsoft question

Given an long string, design an algorithm to find the maximum
repetitive substring. For example, the maximum repetitive substring of "abcabcabc" is "abcabc"

 

1 prefix-tree

2 Build a matrix and find the longest bias

 

13. rotate array using constant spance and o(n) time.

You are given an array with n elements { x1, x2 ... xk ... xn }. You are also given an array location k.

Using constant space and O(n) time, rotate the array such that it contains { xk, xk+1 ... xn, x1, x2 ... xk-1 }.

 

It is like reverse the words in a statement with 2 words

 

14. Searching an element in rotated sorted array?

An element in a sorted array can be found in O(log n) time via binary search. But suppose I rotate the sorted array at some pivot unknown to you beforehand. So for instance, 1 2 3 4 5 might become 3 4 5 1 2. Now devise a way to find an element in the rotated array in O(log n) time.

 

public int rotatedBinarySearch(int[] array, int left, int right, int key)
{
  
if(left==right)
  
{
    
if(array[left]==key)
      
return array[left];
    
else
      
return -1;
  }

  
int middle=(right+left)/2;
  
if(array[left]<array[middle] && array[left]<=key && key<=array[middle])
    
return rotatedBinarySearch(array, left, middle) ;
  
else if(array[left]>array[middle] && (key>array[left] || key<array[middle])
    
return rotatedBinarySearch(array, left, middle);
  
else if(array[middle+1]<array[right] && array[middle+1]<=key && array[right]>=key)
    
return rotatedBinarySearch(array, middle+1, right);
  
else if(array[middle+1]>array[right] && (key>array[middle=1|| key<array[right])
    
return rotatedBinarySearch(array, middle+1, right);
  
return -1;
}

 

 

15. Pretty iterative permutations

So you have an array of N elements (say they are all distinct integers for simplicity) and you want all permutations of K elements (K<=N).

Write iterative algo to print these permutations.
   let a[] represent an arbitrary list of objects to permute
   let N equal the length of a
[]
   create an integer array p
[] of size N+1 to control the iteration     
   initialize p
[0] to 0, p[1] to 1, p[2] to 2, ..., p[N] to N
   initialize index variable i to 
1
   while (i < N) do {
      decrement p
[i] by 1
      if i is odd
, then let j = p[i] otherwise let j = 0
      swap(a
[j], a[i])
      let i 
= 1
      while (p
[i] is equal to 0) do {
         let p
[i] = i
         increment i by 
1
      } // end while (p
[i] is equal to 0)
   } // end while (i < N)
原创粉丝点击