Tribal Fusion Interview Question for Software Engineer / Developers


Country: India
Interview Type: Phone Interview




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

It can be done by performing Counting Sort twice.
For the first time the frequency of each number is counted in another array.
For the second time, we perform counting on the frequencies with same value.
Space complexity = O(4n). Time Complexity = O(5n). So basically both space and time complexities are of the order n.

#include <stdio.h>
# define ARY_SIZE 50
//a[] is input array, d[] is output array
int sortByFreq(int a[], int d[], int len) {  
    int b[len], c[len], i, newLen = 0;
    for(i = 0; i < len; i++) {
        b[i] = 0;
        c[i] = 0;
    }
// Frequency of each element is stored in b[]
    for(i = 0; i < len; i++) 
        b[a[i]]++;                       
// Counting for freq with same value in c[]
    for(i = 0; i < len; i++) 
        c[b[i]]++;                       
// indexing for the output in array d[]
    for(i = len - 2; i > 0; i--)
        c[i] += c[i+1];                  
// d[] is finally created now
    for(i = len - 1; i >= 0; i--) {    
        if(b[a[i]]) {
            d[c[b[a[i]]] - 1] = a[i];
            c[b[a[i]]]--;
            b[a[i]] = 0;
            newLen++;
        }
    }
    return newLen;
}

int main() {
    int len = 0, n;
    int a[ARY_SIZE];
    printf("Enter the numbers of the array:\n");
    while(scanf("%d", &n) == 1)
        a[len++] = n;
    int d[len];
    len = sortByFreq(a, d, len);
    printf("The sorted array in decreasing frequency is:\n");
    for(n = 0; n < len; n++)
        printf("%d  ", d[n]);
    printf("\n");
}

- cooldaa January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will only work when numbers in array in this range of 0 to n-1. This is not specified in question.

- Manish April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will work only when numbers in array are in the range of 0 to n-1. This is not specified in question.

- Manish April 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

whether u want the result to be sorted by occur time or by the original order in the array?

- anonymous January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done with the concept of counting sort.

Pseudo code:- 
a= [3,4,2,5,3,3,4,2,1,5}
int n=a.length();
int B[]=new int[max(a)]; //initialise with zero
for i<- 0 to n-1
    do{
         B[a[i]]++;
         }
int start=0;
while(B!=null) // this whole loop will late o(m^2) time if m is large this method is not good
{ find max in B // in O(m) time m is the maximum element value
Let i th index is max
for(int j=start;j<B[i];j++)
{
          a[j]=i;
}

- Anonymous January 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

We can use the hashing and link list combination here.
First create a hash table and values will be the frequency of no.

eg: a= [3,4,2,5,3,3,4,2,1,5}

hash table of size 5 (biggest no)
let say it is h[5].
h[1] = 0, h[2]=2, h[3]=3, h[4]=2, h[5]=2.

Now iterate the hash table ans use insertion sort using linked list.

- Piyush Beli April 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why r u recommending insertion sort in it

- Lokesh June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use LinkedHashMap

- Ajay May 17, 2012 | 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