## Facebook Interview Question

Software Engineers**Country:**United States

Python solution for decoding ways:

```
def decodeWays(ints):
if not ints or ints.startswith('0'): return 0
dp = [1 for i in xrange(len(ints)+2)]
for i in xrange(len(ints)):
if ints[i] == '0':
if int(ints[i-1]) < 1 or int(ints[i-1]) > 2:
return 0
dp[i+2] = dp[i]
else:
dp[i+2] = dp[i+1]
if 10 < int(ints[i-1:i] + ints[i]) < 27:
dp[i+2] += dp[i]
return dp[-1]
```

Python solution for converting to double linked list:

```
def doConvert(node, isLeft=False):
if not node:
return node
node.prev = doConvert(node.left, True)
node.next = doConvert(node.right, False)
if node.prev: node.prev.next = node
if node.next: node.next.prev = node
return node.next or node if isLeft else node.prev or node
```

Random Index C++ O(n) time and O(n) space

```
int randomIndex(vector<int>& nums)
{
int size = nums.size();
if(size == 0)
return -1;
if(size == 1)
return 0;
vector<int> indices;
int max = INT_MAX;
for(int i=0;i<size;i++)
{
if(nums[i] > max)
{
indices.clear();
max = nums[i];
indices.push_back(i);
}
else if(nums[i] == max)
{
indices.push_back(i);
}
}
if(indices.size() == 1)
return 0;
int rand = rand();
int idx_size = indices.size();
return indices[rand % idx_size];
}
```

All the algo questions were seen except the Random Max Index. Optimal solution is O(n) in time (single pass)and constant extra space.

Generate random max index

Given an array of integers, randomly return an index of the maximum value seen by far.

e.g.

Given [11,30,2,30,30,30,6,2,62, 62]

Having iterated up to the at element index 5 (where the last 30 is), randomly give an index among [1, 3, 4, 5] which are indices of 30 - the max value by far. Each index should have a ¼ chance to get picked.

Having iterated through the entire array, randomly give an index between 8 and 9 which are indices of the max value 62.

Solution:

Looking for interview experience sharing and mentors?

- aonecoding April 17, 2017Visit A++ Coding Bootcamp at aonecode.com.

Taught by experienced engineers/interviewers from FB, Google and Uber,

our ONE TO ONE courses cover everything in an interview including

latest interview questions sorted by companies,

SYSTEM DESIGN Courses (highly recommended for people interviewing with FLAG)

ALGORITHMS (conquer DP, Graph, Greed and other advanced algo problems),

and mock interviews.

Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.

Welcome to email us with any questions. Thanks for reading.