Hackathon Java

1. Unix : Pattern Matching LineCount 

Write a bash shell script to find the count of lines having alphanumeric words from a file.

 

Your script to print the count in the below format:

 

Count: <output>

 

The file name will be provided as command line argument when the script containing your command will run.

 

You can use shell variables (e.g. $0,$1,$2) whichever is applicable for your requirement to provide the command line argument.

 

Note :  The alphanumeric word should have at least one alphabet and one digit if the line is to be counted.

 

Please refer to below sample Input and Output for more details

 

Sample Input 1 :

 

Hello this is unix
My name is XYZ
Hello15 again
Bye Hello
just bye19
123 456
hello hi

 

Output :

 

Count: 2

 

Explanation :

 

In the Input, we have only two lines which have word(s) which include at least one alphabet and one digit, which are highlighted with yellow below,

Hello this is unix
My name is XYZ
Hello15 again
Bye Hello
just bye19
123 456
hello hi

 

Hence, output is 2.

 

Sample Input 2:

 

Hello this is Unix

This is a sample file

123 708

End of file

 

Output:

 

Count: 0

 

    

Solution:

    

grep '[[:alpha:]]' | grep '[[:digit:]]' | echo "Count: $(wc -l)"

 

2.Unix : Print Longest Line

Write the Unix command to print the longest line from a file . 

 

The file name will be provided as command line argument when the script containing your command will run.

 

You can use shell variables (e.g. $0,$1,$2) whichever is applicable for your requirement to provide the command line argument.

 

For more clarity, please refer to the sample input and output below.

 

Sample Input 1 :

 

We are using this file to test some commands.

This is a new file.

File has details of Unix commands.

File is created as a Temporary file.

 

Output :

 

We are using this file to test some commands.

 

 

Sample Input 2 :

 

hi
welcome
hello
hi

 

Output :

 

welcome

 

 

 

 

Solutions:

 

 

awk '{print $0,"|",length}' | awk '{print $NF,"|",$0}' |sort -nr | cut -d'|' -f2 | head  -n1 | cut -c2-

3. Oracle : Department Details

Consider the following tables for the question :

 

Table 1 : Employee

 

Column Name

Datatype

Constraints

EMPID

NUMBER

PRIMARY KEY

EMPNAME

VARCHAR(50)

 


 

Table 2 : Department

 

 

Column Name

Datatype

Constraints

DEPTID

NUMBER

PRIMARY KEY

DEPTNAME  

VARCHAR(50)

 

MAXEMPCOUNT

NUMBER

 

 

 

Table 3 : Allocation

 

Having the following structure:

 

 

Column Name

Datatype

Constraints

ALLOCID   

NUMBER

PRIMARY KEY

DID     

VARCHAR(50)

Foreign KEY ; references the DEPTID of Department table

EID

NUMBER

Foreign KEY ; references the EMPID of the Employee table


     

 

Write an SQL query  to list the name of the departments along with their maximum employee count.

 

 

Example:

 

Employee Table :

 

EMPID

EMPNAME

123

Radhika

456

Thompson

789

Iftikar

345

Raghu

678

Deepak

Department Table:

DEPTID

DEPTNAME

MAXCOUNT

100

Finance

2

200

Marketing

1

300

IT

1

400

Sales

5

 

Allocation Table:

 

ALLOCID

DID

EID

1

100

123

2

200

123

3

100

789

4

200

345

5

300

678

 

Expected Output :

Finance 2
Marketing 1
IT 1
Sales 5

Solutions:

 

select DEptname,MaxEmpCOunt from Department;

 

 

 

4. Oracle : Allocation Details

Write an SQL query  to list the  allocation id, department id and the employee id where the department id is 100 or 200

 

 

Example:

 

Employee Table :

 

EMPID

EMPNAME

123

Radhika

456

Thompson

789

Iftikar

345

Raghu

678

Deepak

Department Table:

DEPTID

DEPTNAME

MAXCOUNT

100

Finance

2

200

Marketing

1

300

IT

1

400

Sales

5

 

Allocation Table:

 

ALLOCID

DID

EID

1

100

123

2

200

123

3

100

789

4

200

345

5

300

678

 

Expected Output :

 

1 100 123
2 200 123
3 100 789
4 200 345

 

Solutions :

 

select allocid,did,eid from allocation where did in (100,200);

 

5. Oracle : Department with Employee Count

Consider the following tables for the question :

 

Table 1 : Employee

 

Column Name

Datatype

Constraints

EMPID

NUMBER

PRIMARY KEY

EMPNAME

VARCHAR(50)

 


 

Table 2 : Department

 

 

Column Name

Datatype

Constraints

DEPTID

NUMBER

PRIMARY KEY

DEPTNAME  

VARCHAR(50)

 

MAXEMPCOUNT

NUMBER

 

 

 

Table 3 : Allocation

 

Having the following structure:

 

 

Column Name

Datatype

Constraints

ALLOCID   

NUMBER

PRIMARY KEY

DID     

VARCHAR(50)

Foreign KEY ; references the DEPTID of Department table

EID

NUMBER

Foreign KEY ; references the EMPID of the Employee table


     

Write an SQL query  to list the  department id along with the count of employees allocated in it.

 

 

Example:

 

Employee Table :

 

EMPID

EMPNAME

123

Radhika

456

Thompson

789

Iftikar

345

Raghu

678

Deepak

Department Table:

DEPTID

DEPTNAME

MAXCOUNT

100

Finance

2

200

Marketing

1

300

IT

1

400

Sales

5

 

Allocation Table:

 

ALLOCID

DID

EID

1

100

123

2

200

123

3

100

789

4

200

345

5

300

678

 

Expected Output :

 

100 2

200 2

300 1

 

Solutions:

select did ,count(*) from allocation group by did;

 

 

 

 

6. Oracle : DeptNames based onEmpCount

Consider the following tables for the question :

 

Table 1 : Employee

 

Column Name

Datatype

Constraints

EMPID

NUMBER

PRIMARY KEY

EMPNAME

VARCHAR(50)

 


 

Table 2 : Department

 

 

Column Name

Datatype

Constraints

DEPTID

NUMBER

PRIMARY KEY

DEPTNAME  

VARCHAR(50)

 

MAXEMPCOUNT

NUMBER

 

 

 

Table 3 : Allocation

 

 

Column Name

Datatype

Constraints

ALLOCID   

NUMBER

PRIMARY KEY

DID     

VARCHAR(50)

Foreign KEY ; references the DEPTID of Department table

EID

NUMBER

Foreign KEY ; references the EMPID of the Employee table


     

Write an SQL query  to list the names of all the departments which has more than 5 employees allocated

 

Example:

 

Employee Table :

 

EMPID

EMPNAME

123

Radhika

456

Thompson

789

Iftikar

345

Raghu

678

Deepak

Department Table:

DEPTID

DEPTNAME

MAXCOUNT

100

Finance

2

200

Marketing

1

300

IT

1

400

Sales

5

 

Allocation Table:

 

ALLOCID

DID

EID

1

100

123

2

200

123

3

100

789

4

200

345

5

100

678

6

200

789

7

100

345

8

100

678

9

100

456

10

200

456

11

400

456

12

400

789

 

Expected Output :

 

Finance

Oracle

 

Solutions

select deptname from department where deptid in (select did 

from allocation group by did having count(*)>5);

 

 

7. Oracle : Emp Details Based on DeptName

Consider the following tables for the question :

 

Table 1 : Employee

 

Column Name

Datatype

Constraints

EMPID

NUMBER

PRIMARY KEY

EMPNAME

VARCHAR(50)

 


 

Table 2 : Department

 

 

Column Name

Datatype

Constraints

DEPTID

NUMBER

PRIMARY KEY

DEPTNAME  

VARCHAR(50)

 

MAXEMPCOUNT

NUMBER

 

 

 

Table 3 : Allocation

 

Having the following structure:

 

 

Column Name

Datatype

Constraints

ALLOCID   

NUMBER

PRIMARY KEY

DID     

VARCHAR(50)

Foreign KEY ; references the DEPTID of Department table

EID

NUMBER

Foreign KEY ; references the EMPID of the Employee table


     

Write an SQL query  to list the  allocation id, employee id and employee name who are allocated to the department with dept name 'Finance'.

 

Example:

 

Employee Table :

 

EMPID

EMPNAME

123

Radhika

456

Thompson

789

Iftikar

345

Raghu

678

Deepak

Department Table:

DEPTID

DEPTNAME

MAXCOUNT

100

Finance

2

200

Marketing

1

300

IT

1

400

Sales

5

 

Allocation Table:

 

ALLOCID

DID

EID

1

100

123

2

200

123

3

100

789

4

200

345

5

300

678

 

Expected Output :

 

1 123 Radhika

3 789 Iftikar

 

 

Solutions

select a.allocid,e.empid,e.empname from employee e inner join 

allocation a on a.eid=e.empid where a.did in ( select deptid 

from department where deptname='Finance');

 

 

8. Java: String Reverse

String Reverse

 

Write main method in Solution class.

In the main method, read a String and print it in the reverse sequence as they appear in the input.
Please note: The reverse string should be printed in lowercase only.

 

Sample input:
WelCome

Output:
emoclew

--------------------------------------------------
Sample code snippet for reference: 
Please use below code to build your solution.
--------------------------------------------------

public class Solution
{

    public static void main(String[] args)
    {
        //code to read values 
        //code to display the result
    }

 }

-------------------------------------------------

Solutions:

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

 

public class Solution {   

public static void main(String[] args)

    {

        Scanner s=new Scanner(System.in);

        String ss=s.nextLine();

        StringBuffer sb=new StringBuffer(ss);

        sb.reverse();

        ss=sb.toString();

        ss=ss.toLowerCase();

        System.out.println(ss);

    }

}

 

 

9. Java: Unique Characters

Unique Characters

 

Write main method in Solution class.

In the main method,  read a string (with numbers and special characters) and print the unique characters present in the given string in the same sequence as they appear(the first occurrence) in the input.

Note : All the characters should  be in lowercase.

 

Consider below sample input and output:

Input:
xperience

Output:

xperinc


--------------------------------------------------
Sample code snippet for reference: 
Please use below code to build your solution.
--------------------------------------------------

public class Solution
{    
    
    public static void main(String[] args)
    {
        //code to read values 
        //code to display the result
    }

    
}

-------------------------------------------------
 

Solutions:

 

 

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

 

public class Solution {

    public static void main(String[] args)

    {

         Scanner s=new Scanner(System.in);

         ArrayList<Character> al =new ArrayList<Character>();

         String ss=s.next();

         for(int i=0;i<ss.length();i++){

             char c=ss.charAt(i);

             if(al.contains(c)){

                 continue;

             }

             else{

                 al.add(c);

             }

         }

         String p="";

         for (char i:al){

             p+=i;

         }

         System.out.println(p);

    }

}

 

 

 

 

 

 

 

10. Java: Device Management

Device Management

 

Create a class Phone with below attributes:
phoneId - int
os - String
brand - String 
price - int

Write getters, setters and parameterized constructor in the above mentioned attribute sequence as required. 

 

Create class Solution with main method. 

Implement two static methods - findPriceForGivenBrand and getPhoneIdBasedOnOs in Solution class.

 

findPriceForGivenBrand method:
This method will take two input parameters - array of Phone objects and string parameter brand. 
The method will return the sum of the price attribute from phone objects for the brand passed as parameter. 
If no phones with the given brand is present in the array of phone objects, then the method should return 0. 

 

getPhoneIdBasedOnOs method:
This method will take a String parameter os, along with the array of Phone objects.
The method will return the phone object, if the input String parameter matches with the os attribute of the phone object and its price attribute is greater than or equal to 50000. If any of the conditions are not met, then the method should return null.

 

Note : No phone object would have the same value for os attribute.
          All phone object would have the price greater than 0.
         All the searches should be case insensitive.

 

These above mentioned static methods should be called from the main method. 

 

For findPriceForGivenBrand method - The main method should print the price as it is if the returned price is greater than 0, or it
should print "The given Brand is not available".

 

For getPhoneIdBasedOnOs method - The main method should print the phoneId of the returned phone object. If the returned value is null
then it should print "No phones are available with specified os and price range".

 

Before calling these static methods in main, use Scanner object to read the values of four Phone objects referring attributes in the above mentioned attribute sequence. 
Next, read the value for brand and os.


Consider below sample input and output:
Input:
111
iOS
Apple
30000
222
android
Samsung
50000
333
Symbian
HTC
12000
444
Paranoid
HTC
89000
Blackberry
aNdRoid

Output:
The given Brand is not available
222

--------------------------------------------------
Sample code snippet for reference: 
Please use below code to build your solution.
--------------------------------------------------

public class Solution
{

    public static void main(String[] args)
    {
        //code to read values 
        //code to call required method
        //code to display the result
    }

    public static int findPriceForGivenBrand(Phone[] phone, String brand)
      {
          //method logic
      }

    public static Phone getPhoneIdBasedOnOs(Phone[] phone, String os)
      {
          //method logic
      }
}

class Phone
{
    //code to build Phone class
}

-------------------------------------------------

Note on using Scanner object:
Sometimes scanner does not read the new line character while invoking methods like nextInt(), nextDouble() etc. 
Usually, this is not an issue, but this may be visible while calling nextLine() immediately after those methods.

Consider below input values:
1001
Savings

Referring below code:

Scanner sc = new Scanner(System.in);
int x = sc.nextInt();
String str = sc.nextLine(); -> here we expect str to have value Savings.Instead it may be "".

If above issue is observed, then it is suggested to add one more explicit call to nextLine() after reading numeric value.

 

Solutions:

 

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

 

public class Solution {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int phoneId;

        String os;

        String brand;

        int price;

        Phone[] p = new Phone[4];

        for (int i = 0; i < p.length; i++) {

            phoneId = sc.nextInt();

            sc.nextLine();

            os = sc.nextLine();

            brand = sc.nextLine();

            price = sc.nextInt();

            sc.nextLine();

            p[i] = new Phone(phoneId, os, brand, price);

        }

        String checkbrand = sc.nextLine();

        String checkos = sc.nextLine();

        int res = findPriceForGivenBrand(p, checkbrand);

        Phone pres = getPhoneIdBasedOnOs(p, checkos);

        if(res!=0)

            System.out.println(res);

        else

            System.out.println("The given Brand is not available");

        if(pres!=null)

            System.out.println(pres.getPhoneId());

        else

            System.out.println("No phones are available with specified os and price range");

        sc.close();

    }

 

    public static int findPriceForGivenBrand(Phone[] phone, String brand) {

        int sum = 0;

        for (int i = 0; i < phone.length; i++) {

            if (phone[i].getBrand().toLowerCase().contains(brand.toLowerCase()))

                sum = sum + phone[i].getPrice();

        }

        return sum;

    }

 

    public static Phone getPhoneIdBasedOnOs(Phone[] phone, String os) {

        Phone temp=null;

        for (int i = 0; i < phone.length; i++) {

            if ((phone[i].getOs().toLowerCase().contains(os.toLowerCase()))&&(phone[i].getPrice()>=50000)) {

                temp=phone[i];

                break;

            }

        }

        return temp;

    }

}

 

class Phone

{

    private int phoneId;

    private String os;

    private String brand;

    private int price;

    public Phone(int phoneId, String os, String brand, int price) {

        super();

        this.phoneId = phoneId;

        this.os = os;

        this.brand = brand;

        this.price = price;

    }

    public int getPhoneId() {

        return phoneId;

    }

    public void setPhoneId(int phoneId) {

        this.phoneId = phoneId;

    }

    public String getOs() {

        return os;

    }

    public void setOs(String os) {

        this.os = os;

    }

    public String getBrand() {

        return brand;

    }

    public void setBrand(String brand) {

        this.brand = brand;

    }

    public int getPrice() {

        return price;

    }

    public void setPrice(int price) {

        this.price = price;

    }

}

 

 

 

 

 

 

 

 

11. Java: Xplore Hackathon Coding Question 4

When data is transmitted over network, it is important to check its validity on the receiving side. This would ensure that data is not altered or corrupted. One of the known approach for validity check is to suffix the data with additional details generated from original data itself.

As original data is always characters, it can be represented as number sequence (ASCII / UTF 8 etc). Hence, data like - ABCD can be represented as 65666768 referring ASCII codes. Hence, some additional data generated out of 65666768 can be suffixed while transmitting this sequence. 

One of the similar approach can be to build a logic where given number sequence is separated by certain digits each. Next, it is identified how many prime numbers are there in those separated numbers. Next, add this total number of prime numbers with second largest prime number in the separated sequence. This new number can be suffixed with #. 

E.g, if we want to to transmit the number sequence - 122423314766053.

Separating two digits each would generate: 12 24 23 31 47 66 05 3. There are total 5 prime numbers and 31 is second largest. Hence, we can suffix 36 with original sequence. The final sequence transmitted will be 122423314766053#36

You need to build a code for this logic. The input will be set of numbers already separated by space. 

The submitted code should return the summation of total no. of prime numbers and second largest prime number.

Consider below example:

Input (input numbers separated by space. The number sequence ends with ; character):

1 0 34 56 76 112 111 17 71 112 113 139;

Answer is: 117 as there are total 4 prime numbers (17, 71, 113, 139) and second largest number is 113 (So in this case, Ans = 4 + 113 = 117)

The code should print the final answer as 117 in this case. (Just print the answer value. Do not print anything else).

Consider the limit on input no. as: 0 <= Input number <= 2,147,483,647. Also, assume that prime number will not be repeated in the input list and each list will have two or more prime numbers. 

Sample input:

233 2435 1123 109 103 4434 2347 993 880 1117 1801 991;

Expected output:

1809

 

Solutions:

 

import java.io.*;

import java.util.*;

import java.text.*;

import java.math.*;

import java.util.regex.*;

 

public class Solution {

    public static void main(String args[] ) throws Exception {

        /* Enter your code here. Read input from STDIN. Print output to STDOUT */

        Scanner sc=new Scanner(System.in);

        String input = sc.nextLine();

        input = input.trim();

        input = input.substring(0,input.length()-1);

        String[] arr=input.split(" ");

        int[] a=new int[arr.length];

        for(int i=0;i<arr.length;i++) {

            a[i]=Integer.parseInt(arr[i]);

        }

        int count =0;

        for(int i=0;i<a.length;i++) {

            if(checkPrime(a[i]))

                count++;

        }

        int[] prime =new int[count];

        int k=0;

        for(int i=0;i<a.length;i++) {

            if(checkPrime(a[i]))

                prime[k++]=a[i];

        }

        for(int i=0;i<prime.length;i++) {

            for(int j=0;j<prime.length-i-1;j++) {

                if(prime[j]<prime[j+1]) {

                    int temp= prime[j];

                    prime[j]=prime[j+1];

                    prime[j+1]=temp;

                }

            }

        }

        System.out.print((prime[1]+count));

        sc.close();

    }

    public static boolean checkPrime(int n) {

        int c=0;

        for(int i=1;i<=n;i++) {

            if(n%i==0)

                c++;

        }

        if(c==2)

            return true;

        else

            return false;

    }

}

 

©2018 by The real one. Proudly created with Wix.com