Tribal Fusion Interview Question Software Engineer / Developers

  • tribal-fusion-interview-questions
    0
    of 0 votes
    8
    Answers

    Given an array of n elements. The array can have duplicates.
    Now sort the array based on frequency of distinct number in array.

    eg: a= [3,4,2,5,3,3,4,2,1,5}
    output = {3, 4,2,5,1} coz, 3 occurs thrice, 4,2,5 occurs twice, so they are retained in order of there occurrence in original array, and at last 1, as it appears least number of time.

    - Varun on January 28, 2012 in India Report Duplicate | Flag
    Tribal Fusion Software Engineer / Developer Algorithm

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 on 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 on 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 on 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 on 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 on 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 on 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 on June 15, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use LinkedHashMap

- Ajay on May 17, 2012 | Flag Reply


Add a Comment
Name:

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

Books

is a comprehensive book walking you through 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