Friday, 23 August 2019

Coins to form a sequence of alternating heads and tails - codility test using java



package com.codility.interview;

public class AlternateCoinSequence {

public static int flip(int ch) {
return (ch == 0) ? 1 : 0;
}

public static int getFlipWithStartingCharcter(int[] coinSeq, int expected) {
int flipCount = 0;
for (int i = 0; i < coinSeq.length; i++) {

if (coinSeq[i] != expected)
flipCount++;

expected = flip(expected);
}
return flipCount;
}

public static int minFlipToMakeStringAlternate(int[] coinSeq) {
return Math.min(getFlipWithStartingCharcter(coinSeq, 0), getFlipWithStartingCharcter(coinSeq, 1));
}

public static void main(String[] args) {
AlternateCoinSequence c = new AlternateCoinSequence();

int[] coinSeq = { 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1 };
  //int[] coinSeq = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 }; --7 min flips
  //int[] coinSeq = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1 }; --9 max flips


System.out.println("Number of min flips required to make coin seq are "+minFlipToMakeStringAlternate(coinSeq));

}

}

Tuesday, 26 March 2019

Registrations and Payment (200 Marks) - Code Challenge on Core Java

Use Case:

 Registrations and Payment (200 Marks)
PKR Pvt Limited is in the business of conducting Java Certification exams. Students interested in appearing for Certification examination are required to fill on-line application form on PKR's website. At the end of successful fill of the application form, each student is assigned a "Roll Number". This registration data is provided which consist of the details of the student - Roll No., Registration Date, Registration Time, Name, Gender.

Student needs to make the payment of examination fees of Rs. 1000 through credit card using "Bill Desk". For making the payment, student needs to provide their "Roll Number".

At the end of registration process, "Bill Desk" sends data of all those students who have made the payment. This data of the students who have made the payment is provided.

PKR Pvt limited has a problem - There is a need to find out those students who have registered on PKR's website (and hence, received the "Roll Number") but have not made the payment.

PKR Pvt Limited has hired you as "Technical Specialist". You are requested to develop an efficient computer program in 'Java' programming language to compare both input data (Registration Data and Bill Desk Data) and generate a report on list of those students who have not made the payment.

Core Java Code:

import java.io.*;
import java.util.*;
public class CandidateCode {
    static String rollno;
    static String registrationdate;
    static String registrationtime;
    static String name;
    static String gender;
    static int L=10;
    private String generateRollNumber() {
        Long n = Math.round(Math.random() * 100000000);
        String rollnumber = Long.toString(n);
        return rollnumber;
    }
   
    static String paymentRollNo[] = new String[L];
    private void paymentMethod(String x) {
        int z = 0;
        paymentRollNo[z]=x;
        System.out.println("Payment of 500 is done successfully for RollNo: "+x);
        z++;
    }
   
/*    private void billDeskData() {
        System.out.println();
    }*/
    public static void main(String args[]) throws Exception {
        CandidateCode cc = new CandidateCode();
        Scanner scanner = new Scanner(System.in);
        int N;
        String M;
        System.out.println("how many students registered are N: ");
        N = scanner.nextInt();
        String registrationDate[] = new String[N];
        String registrationTime[] = new String[N];
        String studentName[] = new String[N];
        String studentGender[] = new String[N];
        String studentRollNo[] = new String[N];
       
        for (int i = 0; i < N; i++) {
            System.out.println("Please Register with following details ");
            System.out.println("Enter your Registration Date ");
            registrationdate = scanner.next();
            registrationDate[i] = registrationdate;
            System.out.println("Enter your Registration Time ");
            registrationtime = scanner.next();
            registrationTime[i] = registrationtime;
            System.out.println("Enter your Name ");
            name = scanner.next();
            studentName[i] = name;
            System.out.println("Enter your Gender ");
            gender = scanner.next();
            studentGender[i] = gender;
            if (registrationDate[i] != null & registrationTime[i] != null & studentName[i] != null
                    & studentGender[i] != null) {
                rollno = cc.generateRollNumber();
                System.out.println("rollno: "+rollno);
                studentRollNo[i] = rollno;
            } else {
                System.out.println("Please Re-Check all details and fill the details Properly");
            }
           
           
            System.out.println(
                    "Payment of Examination Fees 500 Rupees: If you want to continue with Payment now please enter your RollNumber or please enter N to exit");
            M = scanner.next();
            if(M.equals("N"))
            {
                System.out.println("Thanks for registering with us");
            }
            else
            {
                cc.paymentMethod(M);
            }
        }
        for (int j = 0; j < N; j++) {
            //System.out.println("Registration_Data :");
            //System.out.println(N);
            System.out.println(studentRollNo[j] + ", " + registrationDate[j] + ", " + registrationTime[j] + ", "
                    + studentName[j] + ", " + studentGender[j]);
        }
       
        System.out.println("Students details who had made Payment are: ");
        for (int k = 0; k < N; k++) {
            if(paymentRollNo[k] !=null) {
            System.out.println(paymentRollNo[k] + ", "+ studentName[k]);
        }
        }
            System.out.println("Students details who haven't made Payment are: ");
            for (int k1 = 0; k1 < N; k1++) {
                if(paymentRollNo[k1] ==null) {
                System.out.println(studentRollNo[k1] + ","+ studentName[k1]);
            }
        scanner.close();
    }
 }
}