Interview Question for Students


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <math.h>
#define BASE 10
int main()
{
int x,y,z,j,t;
long long num, n;
int i, lastDigit;
int freq[BASE];
printf("Enter frequncy of x y and z");
scanf("%d %d %d", &x,&y,&z);
t=x+y+z;

for(j=1;j<(6*pow(10,t));j++)
{
/* Input number from user */
// printf("Enter any number: ");
//scanf("%lld", &num);

/* Initialize frequency array with 0 */
for(i=0; i<BASE; i++)
{
freq[i] = -10;
// printf("%d",freq[i]);
}

/* Copy the value of 'num' to 'n' */
num = j;
n = num;

/* Run till 'n' is not equal to zero */
while(n != 0)
{
/* Get last digit */
lastDigit = n % 10;

/* Remove last digit */
n /= 10;

/* Increment frequency array */
if(lastDigit==4 || lastDigit==5 || lastDigit==6)
{
if(freq[lastDigit]==-10)
freq[lastDigit] = -1;
freq[lastDigit]++;
}
else
{
num=-1;
break;
}
}
if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0)
printf(" %d\n",num);
if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0)
printf(" %d\n",num);
if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0)
printf(" %d\n",num);
if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0)
printf(" %d\n",num);
if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0)
printf(" %d\n",num);
}


return 0;
}

- Anonymous July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <math.h>
#define BASE 10
int main()
{
    int x,y,z,j,t;
    long long num, n;
    int i, lastDigit;
    int freq[BASE];
    printf("Enter frequncy of x y and  z");
    scanf("%d %d %d", &x,&y,&z);
    t=x+y+z;

    for(j=1;j<(6*pow(10,t));j++)
    {
    /* Input number from user */
   // printf("Enter any number: ");
    //scanf("%lld", &num);
    
    /* Initialize frequency array with 0 */
    for(i=0; i<BASE; i++)
    {
        freq[i] = -10;
       // printf("%d",freq[i]);
    }

    /* Copy the value of 'num' to 'n' */
    num = j;
    n = num; 

    /* Run till 'n' is not equal to zero */
    while(n != 0)
    {
        /* Get last digit */
        lastDigit = n % 10;

        /* Remove last digit */
        n /= 10;

        /* Increment frequency array */
        if(lastDigit==4 || lastDigit==5 || lastDigit==6)
        {
                if(freq[lastDigit]==-10)
                    freq[lastDigit] = -1;
                freq[lastDigit]++;
        }
        else
        {
                num=-1;
                break;
        }
    }
    if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0)
        printf(" %d\n",num);
    if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0)
        printf(" %d\n",num);
    if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0)
        printf(" %d\n",num);
    }
        
    
    return 0;
}

- Anonymous July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
#include <stdio.h> #include <math.h> #define BASE 10 int main() {{{ int x,y,z,j,t; long long num, n; int i, lastDigit; int freq[BASE]; printf("Enter frequncy of x y and z"); scanf("%d %d %d", &x,&y,&z); t=x+y+z; for(j=1;j<(6*pow(10,t));j++) {{{ /* Input number from user */ // printf("Enter any number: "); //scanf("%lld", &num); /* Initialize frequency array with 0 */ for(i=0; i<BASE; i++) {{{ freq[i] = -10; // printf("%d",freq[i]); }}} /* Copy the value of 'num' to 'n' */ num = j; n = num; /* Run till 'n' is not equal to zero */ while(n != 0) {{{ /* Get last digit */ lastDigit = n % 10; /* Remove last digit */ n /= 10; /* Increment frequency array */ if(lastDigit==4 || lastDigit==5 || lastDigit==6) {{{ if(freq[lastDigit]==-10) freq[lastDigit] = -1; freq[lastDigit]++; }}} else {{{ num=-1; break; }}} }}} if(freq[4] ==(x-1) && freq[5]==-10 && freq[6] == -10 && num>0) printf(" %d\n",num); if(freq[5] ==(y-1) && freq[4]==-10 && freq[6] == -10 && num>0) printf(" %d\n",num); if(freq[6] ==(z-1) && freq[5]==-10 && freq[4] == -10 && num>0) printf(" %d\n",num); if(freq[4] ==(x-1) && freq[5]==(y-1) && freq[6]==-10 && num>0) printf(" %d\n",num); if(freq[5] ==(y-1) && freq[6]==(z-1) && freq[4]==-10 && num>0) printf(" %d\n",num); if(freq[6] ==(z-1) && freq[4]==(x-1) && freq[5]==-10 && num>0) printf(" %d\n",num); if(freq[4]==(x-1) && freq[5]==(y-1) && freq[6]==(z-1) && num>0) printf(" %d\n",num); }}} return 0; }}} - Anonymous July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Create an array with x,y,z determines the occurences of 4,5,6 in the array. For example: x=1,y=1,z=1 => numArray = [4,5,6];
- Generate all the subsets of that array. ([4],[5],[6],[4,5],...)
- Generate all the permutations of each subsets ([4,5],[5,4],...)
- Convert each permutations array into a number ([4,5] = 45,...)

static void FindSolutions(int x, int y, int z)
        {
            Dictionary<int, int> numsDict = new Dictionary<int, int>
            {
                {4,x},
                {5,y},
                {6,z}
            };

            int maxDigit = x + y + z;

            int[] allNums = ConvertToArray(numsDict,maxDigit);
            
            // Get all the subsets of the array
            List<int> solutions = new List<int>();
            for (int i = 0; i < (1 << allNums.Length); i++)
            {
                List<int> comb = new List<int>();
                for (int j = 0; j < allNums.Length; j++)
                {
                    if ((i & (1 << j)) > 0)
                    {
                        comb.Add(allNums[j]);
                    }
                }

                if (comb.Count > 0)
                {
                    Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
                }
            }
            
            for (int i = 0; i<solutions.Count; i++)
                Console.WriteLine(solutions[i]);
        }


	//Get all the permutations of an array and turn it into a number
        static void Permute(List<int> solutions, int[] numArray, int l, int r)
        {
            int a;
            if (l == r)
            {
                int num = numArray.ToNumber();
                if (!solutions.Contains(num))
                    solutions.Add(num);
            }
            else { 
                for (a = l; a <= r; a++)
                {
                     Swap(ref numArray[l], ref numArray[a]);
                     Permute(solutions, numArray, l+1, r);
                     Swap(ref numArray[l], ref numArray[a]);
                } 
            } 
        }
        
        static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        static int ToNumber(this int[] numArray)
        {
            double returnNum = 0;
            int arrayLength = numArray.Length;
            for (int i = 0; i < arrayLength; i++)
            {
                returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
            }

            return (int)returnNum;
        }

        static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
        {
            int[] allNums = new int[arrayLength];
            int index = 0;
            foreach (var kvp in numsDict)
            {
                int n = kvp.Value;
                for (int i = 0; i < n; i++)
                {
                    allNums[index] = kvp.Key;
                    index++;
                }
            }

            return allNums;
        }

Then on Main, just call FindSolutions(1,1,1);

- QuangSang July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Create an array with x,y,z determines the occurences of 4,5,6 in the array. (x=1, y=1, z=1 => numArray = [4,5,6]
- Generate all the subsets of that array ([4], [5], [6], [4,5],..)
- Generate all the permutations for each subsets ([4,5],[5,4],...)
- Turn each permutations array into a number ([4,5] = 45,...)

static void FindSolutions(int x, int y, int z)
        {
            Dictionary<int, int> numsDict = new Dictionary<int, int>
            {
                {4,x},
                {5,y},
                {6,z}
            };

            int maxDigit = x + y + z;

            int[] allNums = ConvertToArray(numsDict,maxDigit);
            
            // Get all the subsets of the array
            List<int> solutions = new List<int>();
            for (int i = 0; i < (1 << allNums.Length); i++)
            {
                List<int> comb = new List<int>();
                for (int j = 0; j < allNums.Length; j++)
                {
                    if ((i & (1 << j)) > 0)
                    {
                        comb.Add(allNums[j]);
                    }
                }

                if (comb.Count > 0)
                {
                    Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
                }
            }
            
            for (int i = 0; i<solutions.Count; i++)
                Console.WriteLine(solutions[i]);
        }


	//Get all the permutations of an array and turn it into a number
        static void Permute(List<int> solutions, int[] numArray, int l, int r)
        {
            int a;
            if (l == r)
            {
                int num = numArray.ToNumber();
                if (!solutions.Contains(num))
                    solutions.Add(num);
            }
            else { 
                for (a = l; a <= r; a++)
                {
                     Swap(ref numArray[l], ref numArray[a]);
                     Permute(solutions, numArray, l+1, r);
                     Swap(ref numArray[l], ref numArray[a]);
                } 
            } 
        }
        
        static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        static int ToNumber(this int[] numArray)
        {
            double returnNum = 0;
            int arrayLength = numArray.Length;
            for (int i = 0; i < arrayLength; i++)
            {
                returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
            }

            return (int)returnNum;
        }

        static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
        {
            int[] allNums = new int[arrayLength];
            int index = 0;
            foreach (var kvp in numsDict)
            {
                int n = kvp.Value;
                for (int i = 0; i < n; i++)
                {
                    allNums[index] = kvp.Key;
                    index++;
                }
            }

            return allNums;
        }

Then on Main, just call FindSolutions(1,1,1);

- QuangSang July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

- Create an array with x,y,z determines the occurences of 4,5,6 in the array. (x=1, y=1, z=1 => numArray = [4,5,6]
- Generate all the subsets of that array ([4], [5], [6], [4,5],..)
- Generate all the permutations for each subsets ([4,5],[5,4],...)
- Turn each permutations array into a number ([4,5] = 45,...)

static void FindSolutions(int x, int y, int z)
        {
            Dictionary<int, int> numsDict = new Dictionary<int, int>
            {
                {4,x},
                {5,y},
                {6,z}
            };

            int maxDigit = x + y + z;

            int[] allNums = ConvertToArray(numsDict,maxDigit);
            
            // Get all the subsets of the array
            List<int> solutions = new List<int>();
            for (int i = 0; i < (1 << allNums.Length); i++)
            {
                List<int> comb = new List<int>();
                for (int j = 0; j < allNums.Length; j++)
                {
                    if ((i & (1 << j)) > 0)
                    {
                        comb.Add(allNums[j]);
                    }
                }

                if (comb.Count > 0)
                {
                    Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
                }
            }
            
            for (int i = 0; i<solutions.Count; i++)
                Console.WriteLine(solutions[i]);
        }


//Get all the permutations of an array and turn it into a number
        static void Permute(List<int> solutions, int[] numArray, int l, int r)
        {
            int a;
            if (l == r)
            {
                int num = numArray.ToNumber();
                if (!solutions.Contains(num))
                    solutions.Add(num);
            }
            else { 
                for (a = l; a <= r; a++)
                {
                     Swap(ref numArray[l], ref numArray[a]);
                     Permute(solutions, numArray, l+1, r);
                     Swap(ref numArray[l], ref numArray[a]);
                } 
            } 
        }
        
        static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        static int ToNumber(this int[] numArray)
        {
            double returnNum = 0;
            int arrayLength = numArray.Length;
            for (int i = 0; i < arrayLength; i++)
            {
                returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
            }

            return (int)returnNum;
        }

        static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
        {
            int[] allNums = new int[arrayLength];
            int index = 0;
            foreach (var kvp in numsDict)
            {
                int n = kvp.Value;
                for (int i = 0; i < n; i++)
                {
                    allNums[index] = kvp.Key;
                    index++;
                }
            }

            return allNums;
        }

Then on Main, just call FindSolutions(1,1,1);

- QuangSang July 26, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void FindSolutions(int x, int y, int z)
        {
            Dictionary<int, int> numsDict = new Dictionary<int, int>
            {
                {4,x},
                {5,y},
                {6,z}
            };

            int maxDigit = x + y + z;

            int[] allNums = ConvertToArray(numsDict,maxDigit);
            
            // Get all the subsets of the array
            List<int> solutions = new List<int>();
            for (int i = 0; i < (1 << allNums.Length); i++)
            {
                List<int> comb = new List<int>();
                for (int j = 0; j < allNums.Length; j++)
                {
                    if ((i & (1 << j)) > 0)
                    {
                        comb.Add(allNums[j]);
                    }
                }

                if (comb.Count > 0)
                {
                    Permute(solutions, comb.ToArray(), 0, comb.Count - 1);
                }
            }
            
            for (int i = 0; i<solutions.Count; i++)
                Console.WriteLine(solutions[i]);
        }


//Get all the permutations of an array and turn it into a number
        static void Permute(List<int> solutions, int[] numArray, int l, int r)
        {
            int a;
            if (l == r)
            {
                int num = numArray.ToNumber();
                if (!solutions.Contains(num))
                    solutions.Add(num);
            }
            else { 
                for (a = l; a <= r; a++)
                {
                     Swap(ref numArray[l], ref numArray[a]);
                     Permute(solutions, numArray, l+1, r);
                     Swap(ref numArray[l], ref numArray[a]);
                } 
            } 
        }
        
        static void Swap(ref int a, ref int b)
        {
            int temp = a;
            a = b;
            b = temp;
        }

        static int ToNumber(this int[] numArray)
        {
            double returnNum = 0;
            int arrayLength = numArray.Length;
            for (int i = 0; i < arrayLength; i++)
            {
                returnNum = returnNum + (numArray[i]*Math.Pow(10, arrayLength -i -1));
            }

            return (int)returnNum;
        }

        static int[] ConvertToArray(Dictionary<int, int> numsDict, int arrayLength)
        {
            int[] allNums = new int[arrayLength];
            int index = 0;
            foreach (var kvp in numsDict)
            {
                int n = kvp.Value;
                for (int i = 0; i < n; i++)
                {
                    allNums[index] = kvp.Key;
                    index++;
                }
            }

            return allNums;

}

Then on Main, just call FindSolutions(1,1,1);

- Anonymous July 26, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More