-2

I have a Google foobar challenge:

Write a function called answer(data, n) that takes in a list of less than 100 integers and a number n, and returns that same list but with all of the numbers that occur more than n times removed entirely. The returned list should retain the same ordering as the original list - you don't want to mix up those carefully-planned shift rotations! For instance, if data was [5, 10, 15, 10, 7] and n was 1, answer(data, n) would return the list [5, 15, 7] because 10 occurs twice, and thus was removed from the list entirely.

And this was my answer:

    public static int[] solution(int[] data, int n) {
        // Your code here
        int count,c=0;
        int flag[]=new int[1000];
        int b[]=new int[data.length];
        for(int i=0;i<data.length;i++)
        {   count=0;
            if(flag[(data[i])]==0)
            {
                for(int j=0;j<data.length;j++)
                {
                    if(data[i]==data[j])
                        count++;
                }
                if(count>n)
                    flag[(data[i])]=1; 
                else
                {
                    flag[(data[i])]=2;
                    b[c++]=data[i];
                }
            }
            else if(flag[(data[i])]==2)
            {
                b[c++]=data[i];
            }
        }
        if(c==(data.length))
        {
            return b;
        }
        if(c==0)
        {
            int ne[]=new int[0];
            return ne;
        }
        else
        {
            int ne[]=new int[c];
            for(int k=0;k<c;k++)
            {
                ne[k]=b[k];
            }
            return ne;
        }
    }

It passed 8 test cases but is failing for the last test case and I am not able to figure out what could the test case since that one is a hidden case. Any idea?

1 Answers1

0

Generally, when trying to find a problem, you run tests. I made your code fragment into a class I could teat.

I formatted your code to see more clearly what you were doing. Using more descriptive variable names would help. Also, don't rely on single statements of an if or a for loop not needing braces. The first time you try to add a line of code, forgetting that you left off the braces, well good luck debugging that.

Here's the test class I created. I couldn't find anything after trying about two dozen different values of N and input.

import java.util.Arrays;

public class GoogleChallenge {

    public static void main(String[] args) {
        int[] data = { 5, 10, 15, 10, 7, 888, 999, 999, 0 };
        int n = 1;
        System.out.println("input: n: " + n + " " + Arrays.toString(data));
        System.out.println("Output: " + Arrays.toString(solution(data, n)));
    }

    public static int[] solution(int[] data, int n) {
        // Your code here
        int count, c = 0;
        int flag[] = new int[1000];
        int b[] = new int[data.length];
        for (int i = 0; i < data.length; i++) {
            count = 0;
            if (flag[(data[i])] == 0) {
                for (int j = 0; j < data.length; j++) {
                    if (data[i] == data[j]) {
                        count++;
                    }
                }
                if (count > n) {
                    flag[(data[i])] = 1;
                } else {
                    flag[(data[i])] = 2;
                    b[c++] = data[i];
                }
            } else if (flag[(data[i])] == 2) {
                b[c++] = data[i];
            }
        }
        
        if (c == (data.length)) {
            return b;
        }
        
        if (c == 0) {
            int ne[] = new int[0];
            return ne;
        } else {
            int ne[] = new int[c];
            for (int k = 0; k < c; k++) {
                ne[k] = b[k];
            }
            return ne;
        }
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111