superffeng
BAN USER- 1of 1 vote
AnswersGenerate a number is range (1,n) but not in a list (i,j)
- superffeng in United States for Site reliabilty
for example range is (1,1000), list is [2,3,5,9,199,200,344]| Report Duplicate | Flag | PURGE
Google Site Reliability Engineer Algorithm - 0of 0 votes
AnswersGiven a number represented as an array of digits, plus one to the number.
- superffeng in United States for Site reliabilty
ie. 1000 is [1,0,0,0] result is [1,0,0,1]| Report Duplicate | Flag | PURGE
Google Site Reliability Engineer Algorithm - 1of 1 vote
AnswersHow to check num is power of 2?
- superffeng in United States for Site reliabilty| Report Duplicate | Flag | PURGE
Google Site Reliability Engineer Algorithm - 0of 0 votes
Answershow to design a file system, which data structure?
- superffeng in United States| Report Duplicate | Flag | PURGE
Riverbed Software Engineer / Developer - 0of 0 votes
AnswersHow to check whether a integer is a prime?
- superffeng in United States| Report Duplicate | Flag | PURGE
Riverbed Software Engineer / Developer Algorithm - 0of 0 votes
AnswersAn unsorted array of n integers, and range of integers are 1 to n. In the array, one integer has a duplicate , and one integer is missing. How to find the duplicate number and the missing number?
- superffeng in United States
What if array are sorted?| Report Duplicate | Flag | PURGE
Riverbed Software Engineer / Developer Algorithm - 0of 0 votes
AnswersHow to check a loop in a link list?
- superffeng in United States| Report Duplicate | Flag | PURGE
Riverbed Software Engineer / Developer - 0of 0 votes
AnswersGiven an unsorted integer array, find the first missing positive integer.
- superffeng in United States
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in O(n) time and uses constant space.| Report Duplicate | Flag | PURGE
Model N Applications Developer - 1of 1 vote
AnswersDesign a LRU cache? by O(1) insert, O(1) delete, O(1) search
- superffeng in United States| Report Duplicate | Flag | PURGE
Model N Applications Developer Algorithm - 0of 0 votes
AnswersGive you an array of String,
- superffeng in United States
return number of distinct strings in that array.| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 0of 0 votes
AnswersInterest problems:
- superffeng in United States
when you deposit a mount of money x1 in a one year , then you get amount of money x2 in n years later. the interest rate R, x1 and x2 is known, calculate n. Write a C/C++ API, return the year. The rate is compound interest rate.| Report Duplicate | Flag | PURGE
Google Software Engineer / Developer Algorithm - 0of 0 votes
AnswersIdentify whether an math expression is legal.
- superffeng in United States for Server and tools in microsoft erp
for example: {[3*(3+2)]+1}*5 is legal
{(]3++}+4 is illegal| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Testing - 0of 0 votes
AnswersHow to test a whiteboard?
- superffeng in United States for Server and tools in microsoft erp| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Testing - 0of 0 votes
AnswersReverse the word in a string.
- superffeng in United States for Server and tools in microsoft erp
for example:
"hello world" after is
"olleh dlrow"
desgin test cases| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Algorithm - 0of 0 votes
Answersprint out the start and end index of the subarray that has the largest sum in an integer array.
- superffeng in United States for Server and tools in microsoft erp
Design test case| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Algorithm - 0of 0 votes
Answersreturn the 2nd largest number in an integer array, then design test cases:
- superffeng in United States for Server and tools in microsoft erp| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Algorithm
No, don't need to deep into this problem, just describe a brief idea about what data structure used in the file system.
- superffeng September 27, 2012Thank you, another question, how can you make sure the probability to return a node is 1/n in your function
- superffeng April 21, 2012Yes, array of strings, complexity O(n)
- superffeng April 20, 2012but when return the root node itself in your solution?
- superffeng April 20, 2012Could you describe it how to do in log(n) time?
- superffeng April 20, 2012Could you describe it how to do in log(n) time?
- superffeng April 20, 2012C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
C# code
I calculate the product of the consecutive subsets of digits, then search the product in the remain part of the int array. for example 2*4 =8 , then search whether 8 exists.
public class Program
{
public static bool sameDigit(int[] a, int product, int i, int j,int len)
{
int curFront = i;
int curEnd = j;
// search the fromt part
for (int m = 0; m < curFront; m++)
{
if (a[m] == product)
return true;
}
// serch the end part
for (int n = curEnd + 1; n < len; n++)
{
if (a[n] == product)
return true;
}
return false;
}
public static bool colorfulNum(int[] a)
{
for (int i = 0; i < a.Length; i++)
{
int product = 1;
for (int j = i; j < a.Length; j++)
{
product *= a[j];
// as no digit is greater than 10
if (product > 10)
break;
else
{
if (a[j] == 0 || a[j] == 1 || sameDigit(a, product, i,j, a.Length))
{
Console.WriteLine("found same pair of product. Not colorful");
return false;
}
}
}
}
Console.WriteLine("this is a colorful number");
return true;
}
static void Main(string[] args)
{
int[] a = {3,5,4,2 };
bool b = colorfulNum(a);
Console.WriteLine(b);
}
}
/*Given a binary tree, print out all of its root-to-leaf
paths, one per line. Uses a recursive helper to do the work.*/
void printPaths(struct node* node)
{
int path[1000];
printPathsRecur(node, path, 0);
}
/* Recursive helper function -- given a node, and an array containing
the path from the root node up to but not including this node,
print out all the root-leaf paths.*/
void printPathsRecur(struct node* node, int path[], int pathLen)
{
if (node==NULL)
return;
/* append this node to the path array */
path[pathLen] = node->data;
pathLen++;
/* it's a leaf, so print the path that led to here */
if (node->left==NULL && node->right==NULL)
{
printArray(path, pathLen);
}
else
{
/* otherwise try both subtrees */
printPathsRecur(node->left, path, pathLen);
printPathsRecur(node->right, path, pathLen);
}
}
/* UTILITY FUNCTIONS */
/* Utility that prints out an array on a line. */
void printArray(int ints[], int len)
{
int i;
for (i=0; i<len; i++)
{
printf("%d ", ints[i]);
}
printf("\n");
} http://www.geeksforgeeks.org/archives/6719
For union of two arrays, follow the following merge procedure.
1) Use two index variables i and j, initial values i = 0, j = 0
2) If arr1[i] is smaller than arr2[j] then print arr1[i] and increment i.
3) If arr1[i] is greater than arr2[j] then print arr2[j] and increment j.
4) If both are same then print any of them and increment both i and j.
5) Print remaining elements of the larger array.
#include<stdio.h>
/* Function prints union of arr1[] and arr2[]
m is the number of elements in arr1[]
n is the number of elements in arr2[] */
int printUnion(int arr1[], int arr2[], int m, int n)
{
int i = 0, j = 0;
while(i < m && j < n)
{
if(arr1[i] < arr2[j])
printf(" %d ", arr1[i++]);
else if(arr2[j] < arr1[i])
printf(" %d ", arr2[j++]);
else
{
printf(" %d ", arr2[j++]);
i++;
}
}
/* Print remaining elements of the larger array */
while(i < m)
printf(" %d ", arr1[i++]);
while(j < n)
printf(" %d ", arr2[j++]);
}
/* Driver program to test above function */
int main()
{
int arr1[] = {1, 2, 4, 5, 6};
int arr2[] = {2, 3, 5, 7};
int m = sizeof(arr1)/sizeof(arr1[0]);
int n = sizeof(arr2)/sizeof(arr2[0]);
printUnion(arr1, arr2, m, n);
getchar();
return 0;
}
Time Complexity: O(m+n)
This should be generating a random number
- superffeng September 27, 2012