Tag Archives: Interview Questions

Cron to run every 30 minutes

How will you set up a job to run every 30 minutes, from midnight till 10 in morning, Monday to Friday?

*/30 0-10 * * 1-5 MyJob

Understanding the syntax

* * * * * *
| | | | | |
| | | | | +– Year (range: 1900-3000)
| | | | +—- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +—— Month of the Year (range: 1-12)
| | +——– Day of the Month (range: 1-31)
| +———- Hour (range: 0-23)
+———— Minute (range: 0-59)

More on Cron Jobs

http://www.nncron.ru/help/EN/working/cron-format.htm

Handling Multithreading with Synchronization

In last post I wrote about how can we speed up our processing using multiple threads in Java, basically doing parallel processing using threads. Using multithreading definitely helps speed up processing, but it can also result in corrupting data if not used properly. Let’s look at this example

//A simple Runnable class
public class RunnableTest implements Runnable{
int count=0;
public void test()
{
try {
count++;
Thread.sleep(1000); //This will just introduce some delay
System.out.println("Hello from Runnable:"+count);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public void run() {
test();
}
}
and call it from somewhere like

Runnable r=new RunnableTest();

for(int i=0;i<20;i++)
{
Thread t1=new Thread(r);
t1.start();
}

Output:
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
Hello from Runnable:20
..
..

We are calling run method 20 times which in turn calls test. We have deliberately introduced some delay in test to give it a feel of real method which is taking time to execute some statements. The output depicts that instead of count being printed as 1, 2, 3 and so on, it is 20 for all the calls. Which means that all the threads are trying to execute same block of code simultaneously. Though parallel processing of code is desirable behavior from multithreading, it can cause issues in scenarios mentioned above.

Let’s say in actual code we are trying to write data into a database. Instead of count we have ID. So first thread creates and ID as X. Then it does some processing and creates an insert statement. While it is doing the processing, a second thread starts executing the same code and creates another ID Y which overrides the ID X (same as count is being modified). So when our thread 1 reaches to the code where it is inserting the data, it will try inserting ID as Y (as X is overridden by Y). Then thread 2 comes and again try to insert ID Y, causing an error.

So in real world we can have blocks of code (or complete methods) which we want to make sure does not get executed by multiple threads at the same time. Fortunately Java provides a simple way to do this. The keyword is synchronized.

public void test()
{
synchronized(this){
try {

count++;
Thread.sleep(1000);
System.out.println(“Hello from Runnable:”+count);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Now if we run the same code, we will get

Hello from Runnable:1
Hello from Runnable:2
Hello from Runnable:3
Hello from Runnable:4
Hello from Runnable:5
Hello from Runnable:6

Synchronized block makes sure that only one thread is executing blocked code at a time. Another way to synchronize is define method itself as synchronized.

public synchronized void test()

This will make sure only one thread enters this method at a time.

Multithreading: Runnable vs Thread

What is multithreading? First of all let us understand what is thread? A thread is simply flow of control in a program/ process.

Now Let’s understand what is multithreading? Let’s take an example, there is a code which reads data from a file and uploads it to a database. Let’s say there are 10 such files and each file takes 1 hr to finish data upload. In a single threaded environment, it will simply translate to 10 hrs job as our code will read and upload files one by one.  Whereas when we move to multithreading, that is multiple threads are executing at the same time asynchronously, it will result in parallel processing of these files and the total time taken might be close to time taken by one file only.

Before getting into details lets understand how to implement multithreading in Java? There are 2 ways to achieve this.

1. Extend Thread class
public class ThreadTest extends Thread{

public void test()
{
System.out.println("Hello from Thread");
}
public void run()
{
test();
}
}

2. Implement Runnable interface

public class RunnableTest implements Runnable{
public void test()
{
System.out.println("Hello from Runnable");
}

@Override
public void run() {
test();
}
}

And somewhere in the main method

public static void main(String s[])
{
Thread t1=new ThreadTest();
t1.start();
Thread t2=new Thread(new RunnableTest());
t2.start();
}

As you can see the important method while using threads is “run”. This method gets called automatically by JVM whenever a thread starts.

Note that we do not call the run method directly, as that will be like calling a normal method and will wait for control to come back to calling class. Whereas using start will make sure the control flow continues without waiting for method call return. To understand what I am referring here, lets change the main method slightly

for(int i=0;i<20;i++)
{
Thread t1=new ThreadTest();
t1.start();
Thread t2=new Thread(new RunnableTest());
t2.start();
}

We will something like (no order)

Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable
Hello from Runnable
Hello from Thread
Hello from Thread
Hello from Thread
Hello from Runnable
Hello from Runnable

Now let’s change it to run

for(int i=0;i<20;i++)
{
Thread t1=new ThreadTest();
t1.run();
Thread t2=new Thread(new RunnableTest());
t2.run();
}

Output

Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable
Hello from Thread
Hello from Runnable

So not actually using multithreading here.

Coming back to original example. Now you can see if we write the code for uploading the data from file to run method, and execute it in multithreaded fashion, we will be able to upload files simultaneously.

We have seen 2 ways to create threads in Java, one by extending thread class and other by implementing runnable. Which is better?

Usually we go for implementing runnable. When implementing runnable we can only override run method, and that is the only thing which we need in most of the cases. Whereas when we extent Thread, we can override other methods like start, sleep, stop etc, which is usually not required. Additionally, extending Thread has the obvious implementation that now our class cannot extend any other class (as Java allows only one class to be extended).

Union vs Union All

The simple difference between using Union and Union All is that Union selects only distinct records from both the resultsets, whereas Union All will return everything.

But today I figured out one more use of union All today. In a ms sql query, I was using union to add up resultset of multiple queries, and got this error

“The text data type cannot be selected as DISTINCT because it is not comparable.”

As I knew none of my queries will return duplicate records, I did not need distinct clause. Replacing Union with “Union All” did the trick in this case.

Blue eyed boy- puzzle

This is conversation between two friends Ram and Shyam

Ram: Hey Bro, long time! How are you?
Shyam: I am good, How are you?
Ram: How are three boys, they must have grown up now. How old are they?
Shyam: Hmm, let me put it like this, if you multiply their ages, you will get the product as 36. And if you add their ages, you will get my house number.
Ram: Interesting, give me some more hint?
Shyam: Ok, my oldest son has blue eyes.
Ram: Awesome, so their ages are …

Puzzle: So what is the age of three boys?

———————————————————————

There are three hints

1. Product of the ages is 36.
2. Sum of the ages is same as house number. Interesting, we do not know house number, we will see how to use it.
3. Oldest is blue eyed boy. How is that useful?

Ok, lets start

1. Product of 3 ages is 36, so possible solutions are

1, 1, 36= 38(sum)
1, 2, 18= 21
1, 3, 12=16
1, 4, 9=14
1, 6, 6=13 *
2, 2, 9=13 *
2, 3, 6=11
3, 3, 4=10

2. Sum of the ages is house number
Looking at the above numbers, its clear that all the sums except 13 are unique, so if the house number was anything other than 13, Ram would have answered easily. But as he asked for one more hint, we now know that the house number is 13 and we are left with 2 possible solutions.
1, 6, 6=13 *
2, 2, 9=13 *

3. Oldest boy has blue eyes.
Out of the two options mentioned above, option 2 has a clear cut oldest age, i.e. 2, 2, 9. In case of 1, 6, 6, statement should have been one of the older boys or both old boys.

So ages are – 2, 2, 9

concatenate strings in different rows of a recordset

How to concatenate strings in different rows of a recordset, using a common id?

Example

I have a table (test123) like this

IdValue DataValue
———————
1 aaaaa
2 bbbbb
1 ccccc
2 ddddd

Desired output

IdValue Data_Concatenated
———————-
1 aaaaa,ccccc
2 bbbbb,ddddd

Solution 1: Using sys_connect_by_path

SELECT idvalue ,
SUBSTR(MAX(sys_connect_by_path(datavalue, ‘,’ )),2) data_concatenated
FROM
(SELECT idvalue ,
datavalue ,
row_number() over (partition BY idvalue order by datavalue) row#
FROM test123
)
START WITH row# = 1
CONNECT BY prior row# = row#-1
AND prior idvalue = idvalue
GROUP BY idvalue
ORDER BY idvalue;

Solution 2:

SELECT idvalue, RTRIM (EXTRACT (XMLAGG (XMLELEMENT (“X”, dataVALUE || ‘,’)), ‘/X/text()’),’,’) VALUE
FROM test123
group by IDvalue;

What is Middleware?

If you look around for the term Middleware, you will see it being used widely but unfortunately not explained properly anywhere. What is middleware? If you are working on simple web applications, the possibility is that you have mostly worked on client-server paradigm without worrying about middleware concepts. Middleware genrally comes into picture when we are dealing with multiple(atleast 2) applications. It helps these applications talk to each other, even if these applications are on different platforms, technologies and servers.

How does Middleware do it? Lets take a simple example. We have a college A, which has an inhouse application (colA app), used to track students data, attendence, results etc. Now there is an internet application which is supposed to display results of students online (result.com). Now result.com app needs to communicate to colA app to fetch student result data. We introduce a middleware application which helps these 2 app communicate. Let’s say this is message oriented middleware, so as soon as a new result is available on colA app, it writes it on a queue of middleware app. Now middleware might do some processing on the data if required and add it to another queue, which is listened to by result app.

Why can’t result app directly talk to colA app? It can, but what will happen if complexity increases and we introduce many new applications into the arena. Say we have hundereds of colleges and tens of result apps. And to complicate it further, college apps also need to communicate to each other. Just imagine the comexity that each app is listening to tens of other apps and fetching data from tens of apps. Each app need to worry about security, concurrency, connectivity, handling so much load. Different apps might be using different technologies to make the communication difficult. With introduction of middleware, we are moving all the complexity out of the apps so that they can focus on their core areas and let middleware worry about handling load, setting up clustors, managing security, dealing with different technologies and platforms.

Middleware

Weak Reference

A WeakReference object is same as an ordinary object but differs only when garbage collection comes into picture.

How does garbage collection work?

When JVM is running out of memory, it triggers garbage collection, i.e. a process which reclaims any space used by dead objects. Dead objects can be thought of the ones which are not referenced from anywhere.

Now coming back to WeakReferece, it is a reference which is ignored by GC process. That is, if an object is only referenced by a weak reference, it will still be removed (collected as garbage).

Let’s take an example to clarify the point (http://en.wikipedia.org/wiki/Weak_reference)

import java.lang.ref.WeakReference;

public class ReferenceTest {
public static void main(String[] args) throws InterruptedException {
WeakReference r = new WeakReference(new String(“I’m here”))
WeakReference sr = new WeakReference(“I’m here”);
System.out.println(“before gc: r=” + r.get() + “, static=” + sr.get());
System.gc();
Thread.sleep(100);

// only r.get() becomes null
System.out.println(“after gc: r=” + r.get() + “, static=” + sr.get());

}
}

Output

before gc: r=I’m here, static=I’m here
after gc: r=null, static=I’m here

Practical usage:

When will I need to make an object as WeakReference?

In case there is some data which is good to have in memory but can be GCed if memory requirements occur. For example, say I have a list of Employees and each employee has address. The address details are not referenced as frequently as other details so I will mark the address as WeakReference object, as it can be re-fetched from database if required. And if JVM needs memory, it can reclaim the address space.

Interesting Java Facts 3: Cloning a multidimensional Array

Consider the following code

int arr[][]={{10,20,30},{23, 45, 67,}};
int clone[][]=arr.clone();
clone[0][0]=-1;

Q. What should be the value of arr[0][0] and clone [0][0]?

A. It is -1 for both. Why? Let understand 2 things- clone always make a shallow copy of object  & Array is an object in Java.

Shallow copy: When we clone an object in Java, it only creates duplicate copies of primitive types, and for objects, the reference is copied. For example if an Employee object containing an address object is cloned, for address only reference is copied, that is not bot employee and clone are referencing same address object and any change in address is reflected in both.

We can override the clone method for Employee to make sure whenever a clone is made a new object for Address is created as well and all the values are actually copied one by one to new object. This will make sure both the original object and clone are referencing to their own address and changes in one does not get reflected in other.

Array is an object: In java array is an object, so a multidimensional array is actually an object with multiple objects (arrays) in it.

Putting both the above concepts together, we can figure out that while cloning, only the references of array objects  in multidimensional array are copied to cloned object, that means any change in one will get reflected to another.

Revisiting Max Subarray Problem

Sometime back I discussed and solved a problem to find out a subarray from an array which has the maximum sum- here.

I solved the problem using brute force, that is, all the sub arrays were actually created and maximum sum subarray was found. That solution was created in order of N^2 complexity.

Let’s see how can we improve on our solution using divide and conquer approach.

Array: {-1,2,4,60,1,3,-2,-6,7,-9}

If we divide the original array into two half arrays from mid of the array. one of the following must be true

1. Max Subarray is part of first half {-1,2,4,60,1}

2. Max Sub array is part of second half {3,2,-6,7,-9}

3. Max Subarray passes through the mid of array

Array with maximum sum and passes through mid can be easily figured out in order of N. In this we will simply keep moving left and right from the mid until we hit maximum sum array, so first subarray would be 2+4+60+1+3=70

To solve 1 and 2 we will recursively use the approach with the 2 sub arrays (Repeatedly divide the array and find max having mid of the array).

The problem set is getting reduced to half with every iteration, so Log N iterations will be there in total. At max N elements are added (compared to be -ve), in each iteration, hence the total complexity of the algorithm is (N log N), so we are able to reduce the complexity from N^2 to (N log N) with the help of divide and conquer approach.