-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?

5
  • 2
    and what is the question? Commented Oct 11, 2021 at 7:18
  • Question is in code block at end :D Commented Oct 11, 2021 at 7:21
  • Where does the 1000 magic value come from - are you sure that it's enough? Commented Oct 11, 2021 at 8:09
  • 1
    It would help if you posted actual code rather than something that cannot compile (e.g. int flag[] is C/C++ style; in C# it would be int[] flat). Commented Oct 11, 2021 at 8:12
  • It is in java and in constraint it is said that the id number of any shift cannot be more than 999 and i cant find out in what condition my code is failing... Itis passing 8 test cases but its failing at the 9th one, i need help in finding out that one test case condition Commented Oct 11, 2021 at 18:57

1 Answer 1

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;
        }
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.