Odd’s and Even’s using..

Write a method to print all odd and even numbers of a given number.

Again this can done using

for loop and

package solutions.java;

 * Created by rchouhan on 10/30/14.
public class OddAndEvenNumbers {

    public static void main(String[] args){
        System.out.println("\nPrinting all odd numbers for the given input using for loop: "+ getOddSequenceUsingFor(100));
        System.out.println("\nPrinting all even numbers for the given input using for loop: "+ getEvenSequenceUsingFor(100));
        System.out.println("\nPrinting all odd numbers for the given input using recursion: "+ getOddSequenceUsingRecursion(100));
        System.out.println("\nPrinting all even numbers for the given input using recursion: "+ getEvenSequenceUsingRecursion(100));

    // Using FOR loop

    // Time Complexity is O(n) - linear
    private static String getOddSequenceUsingFor(int input){
        String oddValues = "";
        for(int i=1; i<= input; i +=2){
            oddValues += i + ",";
        return oddValues;

    // Time Complexity is O(n) - linear
    private static String getEvenSequenceUsingFor(int input){
        String evenValues = "";
        for(int i=0; i<= input; i +=2){
            evenValues += i + ",";
        return evenValues;

    // Using Recursion

    // Time Complexity is O(n) - linear
    private static String oddValues = "";
    private static int i =1;
    private static String getOddSequenceUsingRecursion(int input){
        if(input < i){
            return oddValues;
        oddValues += i + ",";
        i +=2;

        return getOddSequenceUsingRecursion(input);

    // Time Complexity is O(n) - linear
    private static String evenValues = "";
    private static int e =0;
    private static String getEvenSequenceUsingRecursion(int input){
        if(input < e){
            return evenValues;
        evenValues += e + ",";
        e +=2;

        return getEvenSequenceUsingRecursion(input);

Reverse a String

Write a method that reverses a given String.

There are 3 ways that I know how you can reverse a String

1) for-loop
2) recursion
3) StringBuilder (applicable only using Java)

public class ReverseString {

public static void main(String[] args){
  System.out.println("\nReverse a String using StringBuilder: " + 
  System.out.println("Reverse a String using Recursion: " + 
  System.out.println("Reverse a String using For Loop: " + 

private static String reverse(String input){
  return new StringBuilder(input).reverse().toString();

private static String reverseUsingRecursion(String input){
  // A recursion should always has an exit condition/statement
  if(input == null || input.length() <= 1){
    return input;
  // put the first character (i.e charAt(0)) to the end.
  // and recurse with 2nd character (.e subString(1)) onwards

  return reverseUsingRecursion(input.substring(1)) + input.charAt(0);

private static String reverseUsingFor(String input){
  if((null == input) || (input.length() <= 1)){
    return input;
  char[] chars = input.toCharArray();
  int rhsIndex = chars.length - 1;
  // iteratively swap until exit condition lhsIndex < rhsIndex is reached
  for(int lhsIndex = 0; lhsIndex < rhsIndex; lhsIndex++){
    char temp = chars[lhsIndex];
    chars[lhsIndex] = chars[rhsIndex];
    chars[rhsIndex--] = temp;
   return new String(chars);

Difference between Block vs Inline vs Block-Inline elements.

Block Elements Inline Elements Inline-Block Elements
Block level elements normally start and end with a new line, when displayed in a browser. A block element has some whitespace above and below it and does not tolerate any HTML elements next to it. Inline elements are normally displayed without line breaks. An inline element has no line break before or after it, and it tolerates HTML elements next to it. An inline-block element is placed as an inline element (on the same line as adjacent content), but it behaves as a block element.
respects all paddings left & right margins and padding, but not top & bottom allow other elements to sit to their left and right.
force a line break after the block element cannot have a width and height set respect top & bottom margins and padding
h1, h2, h3, h4, h5, h6
ol, ul
b, big, i, small, tt
abbr, acronym, cite, code, dfn, em, kbd, strong, samp, var
a, bdo, br, img, map, object, q, script, span, sub, sup
button, input, label, select, textarea
A combination of block and inline is an example of inline-block with style=”display: inline-block”
From a rendering point of view,
<div> == <span style=”display: block”>
From a rendering point of view,
<span> == <div style=”display: inline”>
From a rendering point of view,
<div> == <div style=”display: inline-block”>

See more @ http://dustwell.com/div-span-inline-block.html

Differences Between REST and SOAP

Representational State Transfer Simple Object Access Protocol
Based of JAX-RS Based on JAX-WS
Supports HTTP/HTTPS verbs (GET,POST,PUT,DELETE) Supports HTTP (POST Only) and also SMTP, JMS
Transport mechanism is purely HTTP Transport mechanism is through RPC asynchronous messaging, based on publish/subscribe same as JMS.
Doesn’t support ACID, Transactions Supports ACID, Atomic Transactions with WS-AT
Doesn’t support Messaging Supports Reliable Messaging with WS-RM
Doesn’t support Security, as URI parameters are not secured. Supports enterprise Security with WS-Security. To attach WS-Security SOAP header, implement a javax.xml.ws.handler.soap.SOAPHandler class and set the handler chain for the binding of the service proxy object
Can be tested on the Browser Tested via SOAP UI
Exposes RESOURCES which represent DATA Exposes OPERATIONS which represent LOGIC
Supports multiple data formats (XML,JSON,TEXT,..) Supports only XML
Stateless Communication. This can be tested by stop the server and checking if the interactions are able to survive Or go back and forth the webpage, after restarting the server to see if the previous content loads Stateful Communication. Stop the server and go back and forth the webpage to see if the state exists.
Point to Point Communication over HTTP Loosely coupled distributed messaging via RPC
No Strong Typing required Strong Typing
REST can be consumed by any client, even a web browser with AJAX & JavaScript. SOAP can be consumed only with specific clients who can call its Operations defined in the WSDL/WADL
REST is lightweight SOAP is heavily weighted.
REST doesn’t require any XML processing & parsing. SOAP requires XML processing via xmlns:s=”http://www.w3.org/2001/XMLSchema
REST consumes less bandwidth, doesn’t require a SOAP header for every message. SOAP consumes more bandwidth, it requires a SOAP header for every message/service call.
REST is Safe (All GET operations are safe), but not secured. SOAP are safe and secured.
REST is Synchronous, because HTTP is Synchronous. SOAP is asynchronous, as its based on messaging service.
REST have no Data Contracts. SOAP have Data Contracts for Request and Responses.
REST can’t be governed, meaning that You will not know who (which client) is consuming the services SOAP is governed, as it has the knowledge of the client who is consuming the service through service registry.
REST cannot generate client side details (aka. artifacts) as it doesn’t have a WSDL. SOAP can generate client side artifacts via WSDL/WADL.
Error handling, success and failure is done by tracking HTTP response code. SOAP can use its exception types and codes for Error Handling and success/failure responses.
There are no tokens sent from client to server. SOAP calls are token based. Server acquires token from Client.
All GET operations of REST can be cached. There is no caching in SOAP.
Performance is Better than SOAP here because of Caching. SOAP performance is good
RESTful Web services can be created using Jersey framework, RESTEasy, apache httpclient, java.net package, JAX-RS, Spring 3 MVC Annotations, apache cfx SOAP Web Services can be implemented by defining XSD and then adding dependencies of either apache cfx, JAX-WS, apache axis, WCF (Windows Communication Foundation), Spring WS

Difference between dot ‘.’ and Square Braces ‘[]’ notations

Accessing members with . is called dot notation. Accessing them with [] is called bracket notation.

The dot notation only works with property names which are valid identifier names [spec], so basically any name that would also be a valid variable name (a valid identifier, see also Valid Characters for JavaScript Variable Names) and any reserved keyword [spec].

Bracket notation expects an expression which evaluates to a string (or can be coerced to a string), so you can use any character sequence as property name. There are no limits to what a string can contain.


obj.foo;  // valid
obj.else  // valid, reserved keywords are valid identifier names
obj.42    // invalid, identifier names cannot start with numbers
obj.3foo  // invalid,                ""
obj.foo-bar // invalid, `-` is not allowed in identifier names

obj[42]   // valid, 42 will be coerced to "42"
obj["--"] // valid, any character sequence is allowed
obj[bar]  // valid, will evaluate the variable `bar` and 
          // use its value as property name

Use bracket notation:

  • When the property name is contained in a variable, e.g. obj[foo].
  • The property name contains characters not permitted in identifiers, e.g. starts with a digit, or contains a space or dash (-), e.g. obj["my property"].

Use dot notation: In all other situations.

There is a caveat though regarding reserved keywords. While the specification permits to use them as property names and with the dot notation, not all browsers or tools respect this (notably older IE versions). So the best solution in my opinion is to avoid using reserved keywords for property names or use bracket notation if you cannot.

Square bracket notation allows use of characters that can't be used with dot notation: var foo = myForm.foo[]; // incorrect syntax var foo = myForm["foo[]"]; // correct syntax

The second advantage of square bracket notation is when dealing with variable property names.

for (var i = 0; i < 10; i++) {
  someFunction(myForm["myControlNumber" + i]);

  • Dot notation is faster to write and clearer to read.
  • Square bracket notation allows access to properties containing special characters and selection of properties using variables
  • The bracket notation allows you to access properties by name stored in a variable:
var obj = { "abc" : "hello" };
var x = "abc";
var y = obj[x];
console.log(y); //output - hello

obj.x would not work in this case.


You cannot use . to access array elements. However you can use both . and [] to access object properties.

When trying to access a non-existing object property with . you will get an error but if you try with []instead you will get undefined