Category 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

Division problem

Problem statement- 3 divides 111 , 13 divides 111111 etc.. find a number having all one’s which is shortest divisible by a given number which has 3 as its last digit.

Logic- The challenge here is that you will soon run out of limit for int or long if you keep on increasing the number. So the idea is to divide in a way which you used to do on paper for large numbers

13)111111(8547
104
——-
71
65
——–
61
54
———-
71
71
————
0

Start with 1 (or 11) and keep adding one, keep a count of 1s added, divide by the required number unless the reminder is 0.

Implementation
public static void divide(int num)
{
int division=11;
int count=2;
while(true)
{
if(division%num==0)
{
System.out.println("count:"+count);
break;
}
else
{
int remindor=division%num;
division=remindor*10+1;
count++;
}
}
}

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.

Complex Event Processing

Let’s start from understanding what is an event. The dictionary explains events as “a thing that happens, of importance”. Second part, i.e. “of importance” is a bit tricky, some events can be important for one but not for another. For example clicking anywhere on your computer screen is actually an event, but it does not result into an action, but clicking on some icon is event of importance as that will open up some application. In a banking system an event will be when someone withdraws money, checks balance, closes account or open account etc. In a computer game there will other type of event like, jump, run, hit, kill, fire etc.

Event processing is what should be done when the event occurs. For example, what to do when a specific icon on a window is clicked? What should be done when an event of withdraw money occurs – check if pin/ signature is correct, check if there is sufficient balance, pay money to person, reduce the amount from balance etc.

Now next level is to handle complex events. A complex event is an event which is result of many events occurring in relation to each other forming a definite pattern.

We are receiving following information from a ground
1. A man is kicking a football.
2. There are 2 goal posts set on opposite ends of ground.
3. There are few men in the ground trying to take control of same ball.

We can deduce a complex event – a football match.

This kind of pattern detection and complex event processing is used in Operational Intelligence solutions to identify opportunities and threats. For example banks use this kind of event processing to detect and take action on fraud detection. Say if a credit card transaction seems out of place, the amount deducted is not supported by historical data about the card usage, or the card is being used too frequently or the card is used at a location which is not in proximity of the consumer address, this kind of events, combination can generate an event for “CheckCreditCardFraud” or “StopPayment” or “RaiseAlert”.

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.