Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

January 30, 2018

Some comments can be executed in java

Executable comments:

https://www.geeksforgeeks.org/executable-comments-java/

January 29, 2018

java interview puzzles

http://javadecodedquestions.blogspot.com/2013/01/java-interviews-frequently-asked-puzzles.html

Java Rest web service using jersey


I was facing problems while running the restful api

Adding below import solved the problem
Tomcat version - 8.5
Java - 8


import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Code :

package test;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@Path("/hello")
public class Hello {

@GET
@Produces(MediaType.TEXT_XML)
public String sayHelloXML() {
String resource = "<? xml version='1.0' ?>" + "<hello> Hi Pragya </hello>";

return resource;
}

@GET
@Produces(MediaType.APPLICATION_JSON)
public String sayHelloJSON() {
String resource = null;

return resource;
}


@GET
@Produces(MediaType.TEXT_HTML)
public String sayHello(@QueryParam("name") String name) {
String resource = "<h1> this is HTML method </h1>" + "Hi "+name;

return resource;
}


}


web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>JavaRestAPI</display-name>

<servlet>
<servlet-name>JAVA API</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>test</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAVA API</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

</web-app>


URL:

http://localhost:8000/JavaRestAPI/rest/hello?name=pragya

I am using port 8000 coz 8080 had some other thing running

References: https://www.youtube.com/watch?v=5jQSat1cKMo

January 28, 2018

Big O notation

http://bigocheatsheet.com/

Bubble Sort again !!




References : https://www.youtube.com/watch?v=F13_wsHDIG4

package com.mylearnings;

public class BubbleSort {

public static void main(String[] args) {
int input[] = {5,7,6,8,65,87,12,3,343,44,767,78};
BubbleSort sort = new BubbleSort();
int[] out = sort.bubbleSort(input);
for(int i = 0; i < out.length; i++) {
System.out.println(out[i]);
}
}


public int[] bubbleSort(int inp[]) {
int len = inp.length;
for(int i = 0; i < len; i++) {
// start from 1 else we will get ArrayIndexOutOfBoundsException
for(int j=1; j< len-i; j++) {
if(inp[j-1] > inp[j]) {
int temp = inp[j-1];
inp[j-1] = inp[j];
inp[j] = temp;
}
}
}
return inp;
}
}

Jump Search

package com.mylearnings;

public class JumpSearch {

public static void main(String[] args) {
// TODO Auto-generated method stub

int sortedInput[] = {5,6,12,15,18,25,45,65,70,75,78,80,82,83,85,88,90,98,100,105,107};
JumpSearch se =  new JumpSearch();
System.out.println(se.jumpSearch(sortedInput, 0, 4, 4, 85));
}

public int jumpSearch(int[] input , int start , int end, int jump , int search) {
//int mid = (end - start) /2;
if(end <= input.length) {
if((input[start] >= search) || (search < input[end])) {
for(int i =start ; i <= end ; i++) {
if(search == input[i]) {
return i;
}
}
}else {
return jumpSearch(input , end , end + jump, jump,  search);
}
}
return -1;
}

}

The most optimum value of jump would be sqrt(size of array)

References : 
http://theoryofprogramming.com/2016/11/10/jump-search-algorithm/

Binary Search

package com.mylearnings;

public class BinarySearch {

public static void main(String[] args) {
int input[] = {5,7,6,8,65,87,12,3,343,44,767,78};
int sortedInput[] = {5,6,12,15,18,25,45,65,70,75,78,80,82,83,85,88,90,98,100,105,107};
BinarySearch search  = new BinarySearch();
int index = search.binarySearch(sortedInput, 0, sortedInput.length, 65);
System.out.println(index);
}

public int binarySearch(int[] input, int startIndex, int endIndex, int search) {
if(startIndex <= endIndex) {

int mid= startIndex + (endIndex - startIndex)/2;
int midVal = input[mid];
if(midVal == search) {
//System.out.println("Value found at index : "+mid);
return mid;
}else if(search < midVal) {
return binarySearch(input, startIndex , mid-1, search);
}else {
return binarySearch(input, mid+1, endIndex, search);
}
}

return -1;
}

}

January 23, 2018

Stack of Integers

package com.mylearnings;

public class IntStack {

int size;
int[] stack;
int top;

IntStack() {
size = 50;
top = -1;
stack = new int[50];
}

IntStack(int size) {
this.size = size;
top = -1;
stack = new int[size];
}

public boolean push(int value) {

if (!isFull()) {
stack[++top] = value;
return true;
}
return false;
}

public int pop() {
return stack[top--];
}

public boolean isFull() {
return top == size - 1;
}

public boolean isEmpty() {
return top == -1;
}

}

----------------------------------------------------------------

Main class

package com.mylearnings;

public class IntStackMain {

public static void main(String[] args) {
IntStack stack = new IntStack(10);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());

}

}