practical and interview questions - ashish-ghub/docs GitHub Wiki
www.interviewbit.com/coding-interview-questions/
((largestAndSmallest)) public static void largestAndSmallest(int[] numbers) {
int largest = Integer.MIN_VALUE; int smallest = Integer.MAX_VALUE; for (int number : numbers) { if (number > largest) { largest = number; } else if (number < smallest) { smallest = number; } }
Producer consumer :
import java.util.LinkedList;
public class Threadexample {
public static void main(String[] args) throws InterruptedException { // Object of a class that has both produce() // and consume() methods final PC pc = new PC(); // Create producer thread Thread t1 = new Thread(new Runnable() { @Override public void run() { try { pc.produce(); } catch (InterruptedException e) { e.printStackTrace(); } } }); // Create consumer thread Thread t2 = new Thread(new Runnable() { @Override public void run() { try { pc.consume(); } catch (InterruptedException e) { e.printStackTrace(); } } }); // Start both threads t1.start(); t2.start(); // t1 finishes before t2 t1.join(); t2.join(); } // This class has a list, producer (adds items to list // and consumber (removes items). public static class PC { // Create a list shared by producer and consumer // Size of list is 2. LinkedList<Integer> list = new LinkedList<>(); int capacity = 2; // Function called by producer thread public void produce() throws InterruptedException { int value = 0; while (true) { synchronized (this) { // producer thread waits while list // is full while (list.size() == capacity) wait(); System.out.println("Producer produced-" + value); // to insert the jobs in the list list.add(value++); // notifies the consumer thread that // now it can start consuming notify(); // makes the working of program easier // to understand Thread.sleep(1000); } } } // Function called by consumer thread public void consume() throws InterruptedException { while (true) { synchronized (this) { // consumer thread waits while list // is empty while (list.size() == 0) wait(); // to retrive the ifrst job in the list int val = list.removeFirst(); System.out.println("Consumer consumed-" + val); // Wake up producer thread notify(); // and sleep Thread.sleep(1000); } } } }
}
.
-
query to retrieve duplicate records from a table.
SELECT EmpID, EmpFname, Department COUNT(*) FROM EmployeeInfo GROUP BY EmpID, EmpFname, Department HAVING COUNT(*) > 1;
-
Write a query to retrieve the list of employees working in the same department.
Select DISTINCT E.EmpID, E.EmpFname, E.Department FROM EmployeeInfo E, Employee E1 WHERE E.Department = E1.Department AND E.EmpID != E1.EmpID;
Q24. Write a query to retrieve the last 3 records from the EmployeeInfo table.
SELECT * FROM EmployeeInfo WHERE EmpID <=3 UNION SELECT * FROM (SELECT * FROM EmployeeInfo E ORDER BY E.EmpID DESC) AS E1 WHERE E1.EmpID <=3;
Q25. Write a query to find the third-highest salary from the EmpPosition table.
SELECT TOP 1 salary FROM( SELECT TOP 3 salary FROM employee_table ORDER BY salary DESC) AS emp ORDER BY salary ASC;
-
How to write a query to show the details of a student from Students table whose FirstName starts with ‘K’?
SELECT * FROM Students WHERE FirstName LIKE ‘K%’.
How to display employee records who gets more salary than the average salary in the department? This can be done by this query –
1 Select * from employee where salary>(select avg(salary) from dept, employee where dept.deptno = employee.deptno;
Employee( empid, salary, departId,) Department(departId, deparment name )