Pages

Tuesday, 25 August 2015

Spiral Matrix in Java

Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
package core.geeks;

public class SpriralMatrix {
       public static void main(String[] args) {
              int[][] array = {  {0, 1, 2, 3, 4},
                           {5, 6, 7, 8, 9},
                           {10, 11, 12, 13, 14},
                           {15, 16, 17, 18, 19}};
              printSpiral(array);
       }

       private static void printSpiral(int[][] array) {
              int left=0; int right = array[0].length-1;
              int top = 0; int bottom = array.length-1;
              int count = 0;

              while(right>=left && top <= bottom) {
                    
                     if(count==0) {
                           // print top row
                           for(int i = left;i<=right;i++) {
                                  System.out.print(" "+array[top][i]);
                           }
                           top++;
                     } else if(count==1) {
                          
                           // print right most column
                           for(int i = top;i<=bottom;i++) {
                                  System.out.print(" "+array[i][right]);
                           }
                           right--;
                          
                     } else if(count==2) {
                           // print bottom row in reverse order.
                           for(int i = right;i>=left;i--) {
                                  System.out.print(" "+array[bottom][i]);
                           }
                           bottom--;
                     } else if(count==3) {
                           // print right most column
                           for(int i = bottom;i>=top;i--) {
                                  System.out.print(" "+array[i][left]);
                           }
                           left ++;
                     }
                     count = (count+1)%4;
              }
       }
}

Output:
0 1 2 3 4 9 14 19 18 17 16 15 10 5 6 7 8 13 12 11

Wednesday, 5 August 2015

Data Abstraction

Abstraction is the process of recognizing and focusing on important characteristics of a situation or object and leaving/filtering out the un-wanted characteristics of that situation or object.

Let’s take a person as example and see how that person is abstracted in various situations
· A doctor sees (abstracts) the person as patient.  The doctor is interested in name, height, weight, age, blood group,  previous or existing diseases etc. of a person.

· An employer sees (abstracts) a person as Employee. The employer is interested in name, age,  health, degree of study, work experience etc. of a person.  

It’s through abstraction we define the essential aspects of a system.  The process of identifying the abstractions for a given system is called as Modelling  (or object modelling).

In the above example, the doctor may not be interested in characteristics of a person on which the employer is interested in and vice versa.

Both employer and doctor will not be interested in all the characteristics of a person (like the color of dress the person wears on a particular day, the food the person takes, the relatives of the person etc.).

But however some elements are common to both doctor and the employer (like name, age, height etc.).  This common element gives way to generalization.   i.e., if we eliminate enough details of an abstraction, it become so generalized that it can be applied wide in range of situations.

In real world, there are millions of abstractions possible and many are complex in nature.

The complexities of abstractions are handled by systematically classifying and generalizing the abstractions based on some pre-defined criteria.  This process is known as classification. 

Classificationbuilds up a hierarchy and it called as abstract hierarchy. 


A "person" abstraction for a hospital information system would be different from a person abstraction for a library information system and even with hospital information system, person abstraction may be different for different projects.

Applications of circular and doubly link list

Circular linked list

A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players.

Circular linked list should be used is a timesharing problem solved by the operating system. The OS will pick a user; let it use a small amount of CPU time and then move on to the next user, etc.

Circular linked list is the basic idea of round robin scheduling algorithm.

A circularly linked list may be a natural option to represent arrays that are naturally circular, e.g. the corners of a polygon.

With a circular list, a pointer to the last node gives easy access also to the first node, by following one link. Thus, in applications that require access to both ends of the list (e.g., in the implementation of a queue), a circular structure allows one to handle the structure by a single pointer, instead of two.

A circular list can be split into two circular lists, in constant time, by giving the addresses of the last node of each piece.

Applications of doubly linked

The browser cache which allows you to hit the BACK buttons (a linked list of URLs).
Applications that have a Most Recently Used (MRU) list (a linked list of file names).
A stack, hash table, and binary tree can be implemented using a doubly linked list. 
Undo functionality in Photoshop or Word (a linked list of state).
A great way to represent a deck of cards in a game.

Singly
Doubly
Circular
Concept
One way direction
Two way direction
One way direction in a circle
Has head
Yes
Yes
No-because tail will refer to first node
Has tail
Yes
Yes
Yes
No of Node
1-next node
2-next node & previous node
1-next node
insert()
O(n)
O(1)
O(n)
delete()
O(n)
O(1)
O(1)
Benefit
require small space for each element
allow to traverse the list in both directions
execute to the end can be quickly.

Friday, 31 July 2015

RESTFul vs SOAP

                                SOAP
                        REST

SOAP is a protocol.


REST is an architectural style.

SOAP stands for Simple Object Access Protocol.


REST stands for REpresentational State Transfer.

SOAP can't use REST because it is a protocol.

REST can use SOAP web services because it is a concept and can use any protocol like HTTP, SOAP.

SOAP uses services interfaces to expose the business logic.

REST uses URI to expose business logic.

JAX-WS is the java API for SOAP web services.

JAX-RS is the java API for RESTful web services.


SOAP defines standards to be strictly followed.


REST does not define too much standards like SOAP.

SOAP requires more bandwidth and resource than REST.


REST requires less bandwidth and resource than SOAP.

SOAP defines its own security.

RESTful web services inherits security measures from the underlying transport.


SOAP permits XML data format only.

REST permits different data format such as Plain text, HTML, XML, JSON etc.


SOAP is less preferred.


More preferred than SOAP.

Representational State Transfer

Representational State Transfer (REST) is a software architecture style for building scalable web services. REST gives a coordinated set of constraints to the design of components in a distributed hypermedia system that can lead to a more performant and maintainable architecture.

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.

REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.

REST is a lightweight alternative to mechanisms like RPC (Remote Procedure Calls) and Web Services (SOAP, WSDL, et al.).


RESTful programming is about:

1.Resources being identified by a persistent identifier: URIs are the found everywhere/ubiquitous choice of identifier these days.

2.Resources being manipulated using a common set of verbs:HTTP methods are the commonly seen case - the venerable Create, Retrieve, Update, Delete becomes POST, GET, PUT, and DELETE.

3.The actual representation retrieved for a resource is dependent on the request and not the identifier: use HTTP Accept headers to control whether you want XML, HTTP, or even a Java Object representing the resource.

4.Maintaining the state in the object and representing the state in the representation.

5.Representing the relationships between resources in the representation of the resource: the links between objects are embedded directly in the representation.

6.Resource representations describe how the representation can be used and under what circumstances it should be discarded/refetched in a consistent manner: usage of HTTP Cache-Control headers.