Online Quiz

Mr. Dengklek is taking part in an online quiz. The quiz consists of N multiple-choice questions, conveniently numbered 0 through N-1. For each question there are three possible answers labeled A, B, and C. For each question, exactly one of the three possible answers is correct. You are given the information on correct answers as a String answers. More precisely, answers[i] is the correct answer to the i-th question. This is of course unknown to Mr. Dengklek.Mr. Dengklek did not know the answer to any of the quiz questions. Being desperate, he hacked the website of the quiz. However, the only thing he could obtain was aggregate information on the answers that will be used. More precisely, thanks to the hack Mr. Dengklek now knows how many questions have the answer A, how many have the answer B, and how many have the answer C.When taking the quiz, Mr. Dengklek will be given the questions one at a time, in the same order that is used in answers. For each of the questions, he will use his intuition to pick one of the answers that are still possible. After he picks the answer for a question, the quiz will always show him the correct answer for that question.

Mr. Dengklek has noticed that sometimes he can rule out some of the answers when answering a question. Your method must return a int[] containing N elements. The i-th element in this int[] must be the number of options Mr. Dengklek will choose from when answering the i-th question.
Examples

1)”AAAAA” = >{1, 1, 1, 1, 1 }

Mr. Dengklek knows that A is the correct answer to all questions. Therefore, for each question, he only has one choice: answering A.

2)”AAABBB” = > {2, 2, 2, 1, 1, 1 }

Mr. Dengklek knows that there will be three As and three Bs. For each of the first three questions he has two choices: answering A or B. After answering the third question, he will discover that all As have been used, so for each of the remaining questions he only has one choice: answering B.

3)”CAAAAAC” = > {2, 2, 2, 2, 2, 2, 1 }

4)”BBCA” = > {3, 3, 2, 1 }

5) “BACACABCBBBBCAAAAACCCABBCAA” = >

{3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, 1 }

public class CheatingQuiz {

    int[] howMany(String answers) {
        String ans[] = answers.split("");
        int many[]  = new int[ans.length - 1];
        String used  = ans[ans.length-1];
        int value = 1;
        many[ans.length-2]=value;
        for (int i=ans.length-2;i>=1;i--) {
            if (ans[i].equals(ans[i+1]))
                many[i-1]= value;
            else {
                if (used.indexOf(ans[i])==-1) {
                    used += ans[i];
                    value++;
                }
                many[i-1]= value;
            }
        }
        System.out.println(java.util.Arrays.toString(many));
        return many;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheatingQuiz cq = new CheatingQuiz();
        cq.howMany("AAAAA");
        //cq.howMany("BBBB");
        //cq.howMany("C");
        cq.howMany("AAABBB");
        cq.howMany("CAAAAAC");
        cq.howMany("BBCA");
        cq.howMany("BACACABCBBBBCAAAAACCCABBCAA");
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: