Anda di halaman 1dari 9

Find the duplicate number in a sorted list

1st Round - Written for programming skills - Algorithms


2nd Round - Discussion with Technical Lead - Data structures , scenario.
Oops concept
===================================================================================
=================================================
Demo d = new Demo(); // object

Demo d1; // reference variable

d1 = d; // assigning the value of object to reference variable.

-----------------------------------------------------------------------------------
---------------------------------------------------
Lets assume that we have a class called Employee in Java. This is how you would
create a reference of the Employee class:

Create a reference
Employee e;

Now if you wanted to create an instance or object of the Employee class, you would
do so:

Create an reference and an object


Employee e = new Employee();

This gives a direct answer to your question, but to understand the real difference
between references and objects, you need to understand a little about how memory is
managed in Java

In java, all types are divided into two categories, primitive types which include
int, long, char, boolean and so on, and reference types like String, Date,
BufferedReader and other classes. This difference is based on the manner in which
memory is allocated to the variables of these types. For example,

int b;
int a = 10;

Both these statements cause the same effect, memory wise. A block of memory from
the stack, big enough to hold an int (4 bytes) is allocated to these variables.
This memory is allocated statically, since the size of int and other primitive
types is fixed. (The ??? in the diagram indicate some unknown value since the
integer has not been initialized yet)

Consider the employee class.

Employee e;
Employee emp = new Employee();

These two statements have different effect memory-wise since they are reference
types. The first statement just creates a reference, or a pointer if you will, to
an instance of type Employee. This essentially means, the statement tells the
compiler "e" is going to point (refer) to an Employee object, but right now is
pointing to nothing (null). The interesting part is, the reference itself, that is
"e" is stored on the stack statically. So, here you create just the reference.

The second statement however, does more than this. "emp" is allotted memory as a
reference as in the previous case, but the use of new keyword creates an object and
allots memory to it on the heap, at runtime, i.e dynamically. This statement tells
the compiler "emp" is going to refer to the Employee object that will be created
as a result of the new keyword. So, here you create a reference and an object that
the reference variable refers to.

-----------------------------------------------------------------------------------
-----------------------------------------------
MyClass obj1 = null;
A reference variable named obj1 of type MyClass refers to nothing(NULL).

List<MyClass> testList = new ArrayList<MyClass>();


We declare a reference variable called testList of type List and assign it to some
newly created ArrayList object in heap.

testList.add(obj1);
List testList's first element is assigned the same reference which obj1 holds
currently viz. NULL.

obj1 = new MyClass();


We created a new MyClass object in heap and assigned obj1 with its reference. But
we haven't assigned any new reference to the List testList's first element, which
was assigned the NULL reference, so it still points nowhere

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

package ds.singlelinkedlist;

public class Node {


public int data;
public node next;

public void displayNode(){


System.out.println("{"+data+"}");
}
}

----------------------------------------
package ds.singlelinkedlist;

public class singleLinkedList {


private Node first;

public singlyLinkedList() {

public boolean isEmpty() {


return (first==null);
}

// used to insert at the beginning of the list


public void insertFirst(int data) {
Node newNode = new Node();
newNode.data = data; newNode = Data and next point to be old first
newNode.next = first;
first = newNode;
}
public Node deleteFirst() {
Node temp = first;
first = first.next;
return temp;
}

public void displayList() {


System.out.println("List(first --> last)");
Node current = first;
while(current!=null){
current.displayNode();
current = current.next;
}

System.out.println();

// insert at last of list


public void insertLast(int data) {

Node current = first;


while(current.next! = null) {
current = current.next; loop untill current.next is null
}

Node newNode = new Node();


newNode.data = data;
current.next = newNode;
}
}

--------------------------------------------------------
package ds.singlelinkedlist;
public class App {
SinglyLinkedList mylist = new SinglyLinkedList();
mylist.insertFirst(100);
mylist.insertFirst(50);
mylist.insertFirst(99);
mylist.insertFirst(88);

mylist.insertLast(999999999999999);

mylist.displaylist();
}
}

output
-----------------------------------------------------
List(first --> last)
{100}
{50}
{99}
{88}

=============================================================

package ds.circularlinkedlist;

public class CircularLinkedList {

private Node first;


private Node last;

public CircularLinkedList() {
first = null;
last = null;

}
private boolean isEmpty() {
return first=null;
}

public void insertFirst(int data) {


Node newNode = new Node();
newNode.data = data;

if(isEmpty()) {
last = newNode;
}
newNode.next = first; // newNode --> old First
first = newNode; // first Place
}

public void insertLast(int data){


Node newNode = new Node();
newNode.data = data;

if(isEmpty()){
first = newNode;
} else {
last.next = newNode; // next value of the last node will point to the new node
last = newNode; // we make new node we created be the last one in the list

}
}

public int deleteFirst() {

int temp = first.data;

if(first.next == null) {
last = null;
}
first = first.next; // first will point to old's next value
return temp;
}

public void displayList() {


System.out.println("List (first --> last)");

Node current = first;


while(current != null) {
current.displayNode();
current = current.next;
}
System.out.println();
}

----
package ds.singlelinkedlist;
public class App {
CircularLinkedList mylist = new CircularLinkedList();
mylist.insertFirst(100);
mylist.insertFirst(50);
mylist.insertFirst(99);
mylist.insertFirst(88);

mylist.insertLast(999999999999999);

mylist.displaylist();
}
}

output
-----------------------------------------------------
List(first --> last)
{88}
{99}
{50}
{100}
{999999999}

=============================================================

Double LinkedList

package ds.doublelinkedlist;

public class Node {


public int data;
public node next;
public Node previous;

public void displayNode() {


System.out.print("{"+ data + "}");
}
}

----
package ds.doublelinkedlist;

public class DoublyLinkedList {

private Node first;


private Node last;

public DoubleLinkedList() {
this.first = null;
this.last = null;
}

public boolean isEmpty() {


return first == null;
}

public void insertFirst(int data) {


Node newNode = new Node();
newNode.data = data;

if(isEmpty()) {
last = newNode; // if empty, last will refer to the new node being created
}else{
first.previous = newNode;
}

newNode.next = first; // new node's next field will point to old first
this.first = newNode;
}

public void insertLast(int data) {

Node newNode = new Node();


newNode.data = data;

if(isEmpty()) {
first = newNode;
}else{
last.next = newNode; // assign old last to new node
newNode.previous = last; // the old last will be the new node privous
}

last = newNode; // linkedlist's last field should point to new node


}

// assume non-empty list

public Node deleteFirst() {

Node temp = first;


if(first.next == null) { // only one item in the list
last = null;

} else {
first.next.previous = null; // list first node will point to null
}

first = first.next; // assigning refrence of the node following old first node to
the first field in the LL object
return temp; // return deleted old first node

}
// assume non-empty list

public Node deleteLast() {


Node temp = last;
if(first.next == null) {
first = null;
} else {
last = last.previous;
return temp;

}
last = last.previous;
return temp;

}
// assume non-empty list

public boolean insertAfter(int key, int data) {


Node current = first; // start from the beginning of list
while(current.data !=key) { // as long as not fount key in particular node
current = current.next;
if(current == null) {
return false;
}
}

Node newNode = new Node();


newNode.data = data;

if(current == last) {
current.next = null;
last = newNode;
} else {
newNode.next = current.next; // new node's next field should point to the node
ahead of the current
current.next.previous = newNode; //the node ahead of current, it's previous field
should point to new node

newNode.previous = current;
current.next = newNode;

return true;
}

// assume non-empty list


public Node deleteKey(int Key) {

Node current = first; // start from the beginning


while(current.data != key) {
current = current.next;
if(current == null) {
return null;
}
}
if(current == first) {
first = current.next; // make first field point to the node following current
since current
} else {
current.previous.next = current.next;

}
if(current == last){
last = current.previous;
} else {
current.next.previous = current.previous;

}
return current;
}

public void displayFOrward() {


System.out.print("List (first --> last):");
Node current = first;
while(current!= null) {
current.displayNode();
current = current.next;
}
System.out.println();
}

public void displayBackward() {

System.out.print("List (last --> first):");


Node current = last;
while(current!= null) {
current.displayNode();
current = current.previous;
}
System.out.println();
}

------

package ds.doublelinkedlist;

public class App {

public static void main(String[] args) {

DoublyLinkedList dl = new DoublyLinkedList();


dl.insertFirst(22);
dl.insertFirst(44);
dl.insertFirst(66);
dl.insertLast(11);
dl.insertLast(33);
dl.insertLast(55);
dl.displayForward();
dl.displayBackward();
dl.deleteFirst();
dl.deleteLast();
dl.deleteKey(11);
dl.displayForward();
dl.insertAfter(22, 77);
dl.insertAfter(33, 88);
dl.displayForward();
}
}

output

List {first --> Last} : { 66 } { 44 } { 22 } { 11 } { 33 } { 55 }

List {Last --> Frist} : { 55 } { 33 } { 11 } { 22 } { 44 } { 66 }

List {first --> Last} : { 44 } { 22 } { 33 }

List {first --> Last} : { 44 } { 22 } { 11 } { 33 } { 55 }

Anda mungkin juga menyukai