Given an array with duplicate elements give an algorithm to get the count of distinct elements in the array


public class ArrayHandler {
    public static void main(String[] args) {

        int[] intArray = {1, 2, 3, 4, 5, 3, 1};
        Set valueSet = new HashSet();
        for (Integer intValue : intArray) {
            valueSet.add(intValue);
        }
        System.out.println("No of distinctive elements are " + valueSet.size());
    }
}

Advertisements

Find first Non repeating char in a String

Given a string, return the first NON-repeating character that occurs in the string. EX: “adzbdcab” returns ‘z’.


public class StringHandler {

    public static void main(String[] args) {

        String input = "adzbdcab";
        char[] inputCharArray = input.toCharArray();
        for (char inputChar : inputCharArray) {
            if (input.indexOf(inputChar) == input.lastIndexOf(inputChar)) {
                System.out.println("First NON-repeating character is " + inputChar);
                break;
            }
        }

    }
}

How to do versioning in REST API

We can versionize REST Api in any one of the following ways.

1. Use the version in the REST Service URI as below

/api/v1/users

2. Pass the service version as a query param to the actual service as below

/api/users?version=v1
/api/users?v=v1

3. Use the date as version and pass it to the REST Api

/api/users?version=date

4. Using the customized accept header as like below

 @GET
    @Produces("application/vnd.bala-v1+json")
    @Path("version")
    public Response getVersion1() {
       String test="This is one of simple api to test. The version is 1";
       return Response.ok().entity(test).build();
    }

    @GET
    @Produces("application/vnd.bala-v2+json")
    @Path("version")
    public Response getVersion2() {
        String test="this is one of simple api to test. The version is 2";
        return Response.ok().entity(test).build();
    }

Then call the service as like below and pass the version in the accept header

curl --cookie "cookie name: cookie value" -H "accept:application/vnd.bala-v2+json" -X GET http://localhost:8080/api/server/version

5. Custom request header
Use the custom request header and pass the version in the http headers as like below. Note that your service should consume the api-version from the header and do the needful

curl --cookie "" -H "api-version:2" -X GET http://localhost:8080/api/users

Kanban board

A Kanban board is a work and workflow visualization tool that enables you to optimize the flow of your work

Below is one of the nice example of a Kanban board.

simple-physical-board-w-card-types

This is widely used in all the companies along with Agile Development.

If we want to maintain a personal Kanban board for managing your personal tasks then we could use the below site

KanbanFlow

Docker Checking logs

Check all the logs:
docker-compose -f <DOCKER_COMPOSE_FILE_NAME> logs

Check logs for a particular container:
docker-compose -f <DOCKER_COMPOSE_FILE_NAME> logs <CONTAINER_NAME>

We can also use the below command to tail the logs
docker-compose -f <DOCKER_COMPOSE_FILE_NAME> logs -f <CONTAINER_NAME>

docker-compose -f <DOCKER_COMPOSE_FILE_NAME> logs –follow <CONTAINER_NAME>

Check the log by container name:

  • Get the container name by running the below command
    docker ps
  • Then get the container name of the service for which we want to check the logs
    docker logs <>