Tuesday, August 28, 2012

Interview questions

Eureka Server

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing. At Netflix, a much more sophisticated load balancer wraps Eureka to provide weighted load balancing based on several factors like traffic, resource usage, error conditions etc to provide superior resiliency.


What is the difference between http POST,PUT and PATCH methods?

POST -
HTTP.POST can be used when the client is sending data to the server and the server will decide the URI for the newly created resource.

PUT - More or less similar to POST the only difference is its idempotent. So multiple PUT request should not alter the state or result.

PATCH - HTTP.PATCH can be used when the client is sending one or more changes to be applied by the the server. The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI. The set of changes is represented in a format called a patch document.

PATCH is used for partial updation of resource.

How transaction are handled in Spring ?

https://docs.spring.io/spring/docs/2.5.x/reference/transaction.html

Data structure related

Design a data structure for the following operations:
I. Enqueue
II. Dequeue
III. Delete a given number(if it is present in the queue, else do nothing)
IV. isNumberPresent
All these operations should take O(1) time


Solution : Use LinkedHashMap of java.

package edu.example.datastructure;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class EfficientQueue  {
   
    private final Map<Integer,Integer> map;
   
    EfficientQueue() {
        map = new LinkedHashMap<Integer, Integer>();
    }
   
    public void enqueue(Integer i) {
        map.put(i, i);
    }
   
    public Integer remove(Integer i) {
        return map.remove(i);
    }
   
    public Integer dequeue() {
        Iterator<Entry<Integer, Integer>> iter = map.entrySet().iterator();
        if (iter.hasNext())
            return map.remove(iter.next().getKey());
        else
            return null;
    }
   
    public boolean isPresent(Integer i) {
        return map.get(i) == null ?false:true;
    }
   
    public boolean hasNext() {
        return !map.isEmpty();
    }

   

   

}

Friday, August 10, 2012

Database notes



How to find nth highest or top n rows from a table

The question has two type of solution if we do not use the rowid or top operator.

One is with correlated sub query -

select distinct a.salary from example.employee a where 3 >= (select count(distinct salary) from example.employee b where a.salary <= b.salary) order by a.salary desc;

Since correlated sub query performed poorly since for every row of outer query the inner query executes once.

The better solution can be by querying using joins.

select distinct a.salary from example.employee a inner join example.employee b on a.salary<=b.salary group by a.salary having count(a.salary)<= 3 order by a.salary desc 


Difference between IN and EXISTS clause in SQL

In many cases IN and EXISTS return the same result though there is a difference between them



When you write a query using the IN clause, you're telling the rule-based optimizer that you want the inner query to drive the outer query (think: IN = inside to outside). For example, to query the 14-row EMP table for the direct reports to the employee KING, you could write the following:
select ename from emp e
    where mgr in (select empno from emp where ename = 'KING');
Here's the EXPLAIN PLAN for this query:
OBJECT     OPERATION
---------- ----------------------------------------
                 SELECT STATEMENT()
                  NESTED LOOPS()
EMP                TABLE ACCESS(FULL)
EMP                 TABLE ACCESS(BY INDEX ROWID)
PK_EMP               INDEX(UNIQUE SCAN)
This query is virtually equivalent to this:
select e1.ename from emp e1,(select empno from emp where ename = 'KING') e2
    where e1.mgr = e2.empno;
You can write the same query using EXISTS by moving the outer query column to a subquery condition, like this:
select ename from emp e
    where exists (select 0 from emp where e.mgr = empno and ename = 'KING');
When you write EXISTS in a where clause, you're telling the optimizer that you want the outer query to be run first, using each value to fetch a value from the inner query (think: EXISTS = outside to inside).
The EXPLAIN PLAN result for the query is:
OBJECT     OPERATION
---------- ----------------------------------------
                 SELECT STATEMENT()
                  FILTER()
EMP                TABLE ACCESS(FULL)
EMP                 TABLE ACCESS(BY INDEX ROWID)
PK_EMP               INDEX(UNIQUE SCAN)
This is virtually similar to the PL/SQL code:
set serveroutput on;
declare
    l_count integer;
begin
    for e in (select mgr,ename from emp) loop
        select count(*) into l_count from emp
         where e.mgr = empno and ename = 'KING';
        if l_count != 0 then
            dbms_output.put_line(e.ename);
        end if;
    end loop;
end;
To determine which clause offers better performance in rule-based optimization, consider how many rows the inner query will return in comparison to the outer query. In many cases, EXISTS is better because it requires you to specify a join condition, which can invoke an INDEX scan. However, IN is often better if the results of the subquery are very small. You usually want to run the query that returns the smaller set of results first.