Posts

Java Program to find Second Largest Number in an Array

Solution: Before diving into the solution, let me tell you this the most common question which gets asked in the most coding rounds and if you are being lazy to practice and learn this now, you are fooling yourself. Learn the logic behind this and you'll also realize after cracking the logic of this code you have learned a beautiful algorithm from DSA, but you have to figure out that yourself. public class SecondLargestInArrayExample{ public static int getSecondLargest(int[] a, int total){ int temp; for (int i = 0; i < total; i++)      {           for (int j = i + 1; j < total; j++)           {                if (a[i] > a[j])                {                    ...

Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.

This one is little tricky problem. I tried to use subquery approach to solve this problem.   +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Id is the primary key column for this table. For example, after running your query, the above  Person  table should have the following rows: +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+ Note: Your output is the whole  Person  table after executing your sql. Use  delete  statement. Solution: DELETE FROM Person WHERE Id NOT IN ( SELECT p . id FROM ( SELECT MIN (Id) as id FROM Person GROUP BY Email) as p);

Combine Tables. [Leetcode Problem]

  1. Combine Tables. [Leetcode problem]  T able:   Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ PersonId is the primary key column for this table. Table:  Address +-------------+---------+ | Column Name | Type | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ AddressId is the primary key column for this table.   Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people: FirstName, LastName, City, State Ref: https://leetcode.com/problems/combine-two-tables/  Solution:   A common mistake would be to use INNER JOIN and not LEFT (or RIGHT) JOIN and ending up omitting records with no addresses but a...

Implement UNIX system calls like ps, fork, join, exec family, and wait for process management (use shell script/ Java/ C programming).

//A small example to show how to use execvp() function in C. We will have two .C files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by calling execvp() function in execDemo.c . //EXEC.c  #include<stdio.h>  #include<unistd.h>  int main()   {        int i;       printf("This  is EXEC.c, called by execvp() ");        printf("\n");       return 0;   }  Now,create an executable file of EXEC.c using command  gcc EXEC.c -o EXEC //execDemo.c #include<stdio.h> #include<stdlib.h>  #include<unistd.h>  int main()  {     //A null terminated array of character      //pointers       char *args[]={"./EXEC",NULL};       execvp(args[0],args);     /*All statements are ignored after execvp() call as this whole process(execDemo.c) is replaced by ...

Write a Java program for pass-II of a two-pass macro-processor. The output of assignment-3 (MNT, MDT and file without any macro definitions) should be input for this assignment.

import java.io.*; import java.util.HashMap; import java.util.Vector; public class macroPass2 { public static void main(String[] Args) throws IOException{ BufferedReader b1 = new BufferedReader(new FileReader("intermediate.txt")); BufferedReader b2 = new BufferedReader(new FileReader("mnt.txt")); BufferedReader b3 = new BufferedReader(new FileReader("mdt.txt")); BufferedReader b4 = new BufferedReader(new FileReader("kpdt.txt")); FileWriter f1 = new FileWriter("Pass2.txt"); HashMap<Integer,String> aptab=new HashMap<Integer,String>(); HashMap<String,Integer> aptabInverse=new HashMap<String,Integer>(); HashMap<String,Integer> mdtpHash=new HashMap<String,Integer>(); HashMap<String,Integer> kpdtpHash=new HashMap<String,Integer>(); HashMap<String,Integer> kpHash=new HashMap<String,Integer>(); HashMap<String,Integer> macroNameHash=new HashMap<String,Int...

Design suitable data structures and implement pass-I of a two-pass macro-processor using OOP features in Java.

  /* Problem Statement: Design suitable data structures and implement pass-I of a two-pass macro-processor using OOP features in Java */ import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; public class macroPass1 { public static void main(String[] Args) throws IOException{ BufferedReader b1 = new BufferedReader(new FileReader("input.txt")); FileWriter f1 = new FileWriter("intermediate.txt"); FileWriter f2 = new FileWriter("mnt.txt"); FileWriter f3 = new FileWriter("mdt.txt"); FileWriter f4 = new FileWriter("kpdt.txt"); HashMap<String,Integer> pntab=new HashMap<String,Integer>(); String s; int paramNo=1,mdtp=1,flag=0,pp=0,kp=0,kpdtp=0; while((s=b1.readLine())!=null){ String word[]=s.split("\\s"); //separate by space if(word[0].compareToIgnoreCase("MACRO")==0){ flag=1; if(word.length<...

Abstract class and method in Java.

Abstract Class:           An abstract class is something which is incomplete and you can not create an instance of the abstract class. If you want to use it you need to make it complete or concrete by extending it. It is created using abstract keyword. This class can contain abstract and non abstract methods. Before learning about abstract class and abstract I recommend you to first learn about abstraction concept in Java. Few important things to remember about abstract class: An abstract class must be declared with an abstract keyword. It can have abstract and non-abstract methods. It cannot be instantiated. It can have constructors and static methods also. It can have final methods which will force the subclass not to change the body of the method. Example of Abstract class with abstract method: abstract   class  Animal{      abstract   void  run();   }   class  Horse  extends ...