From 464374e49d9aafddb21ff2a8add851934d6995f0 Mon Sep 17 00:00:00 2001 From: hussien alrubaye Date: Sat, 13 Mar 2021 20:37:49 -0500 Subject: [PATCH 1/2] add interview questions --- CodingInterviews/.classpath | 10 ++ CodingInterviews/.project | 17 ++++ .../.settings/org.eclipse.jdt.core.prefs | 14 +++ CodingInterviews/src/com/bst/BST.java | 57 +++++++++++ CodingInterviews/src/com/bst/Node.java | 13 +++ .../src/com/linkedList/LinkedListManager.java | 42 ++++++++ .../src/com/linkedList/LinkedListU.java | 31 ++++++ .../src/com/linkedList/ListNode.java | 12 +++ CodingInterviews/src/com/problem1/Cell.java | 15 +++ .../src/com/problem1/ShortPath.java | 71 ++++++++++++++ .../src/com/problem11/TreeNetwork.java | 68 +++++++++++++ .../src/com/problem12/Mirror.java | 63 ++++++++++++ .../src/com/problem13/GraphLevel.java | 90 ++++++++++++++++++ .../src/com/problem14/FirendCircle.java | 70 ++++++++++++++ .../src/com/problem15/WordDistance.java | 72 ++++++++++++++ .../src/com/problem16/WordSimilar.java | 46 +++++++++ .../src/com/problem17/WordBreak.java | 88 +++++++++++++++++ .../src/com/problem18/SprialMatrix.java | 63 ++++++++++++ CodingInterviews/src/com/problem2/GCell.java | 22 +++++ .../src/com/problem2/TripPlaner.java | 67 +++++++++++++ .../src/com/problem20/TopMoviesInStream.java | 36 +++++++ .../src/com/problem21/MerageLinkedList.java | 29 ++++++ .../com/problem3/FindPathBetweenTwoNodes.java | 73 ++++++++++++++ .../src/com/problem4/SimilarTree.java | 63 ++++++++++++ .../src/com/problem5/LCSOnes.java | 68 +++++++++++++ .../src/com/problem6/BinarySearch.java | 52 ++++++++++ CodingInterviews/src/com/problem7/LCS.java | 44 +++++++++ CodingInterviews/src/com/problem8/Stair.java | 15 +++ .../src/com/problem8/StairCaseDemo.java | 41 ++++++++ .../src/com/problem9/Parentheses.java | 55 +++++++++++ .../src}/collection/com/HashMapClass.java | 0 .../src}/collection/com/HashSetClass.java | 0 .../collection/com/LinkListArrayList.java | 0 .../src}/collection/com/Student.java | 0 .../src}/collection/com/TreeSetExample.java | 0 .../src}/collection/com/VectorExample.java | 0 .../src}/collection/com/myQueue.java | 0 .../src}/collection/com/myStack.java | 0 .../src}/com/ds/DynamicArray.java | 0 .../src}/com/ds/DynamicArrayDemo.java | 0 .../src}/com/ds/LinkedListD.java | 0 .../src}/com/ds/LinkedListDDemo.java | 0 .../src}/com/ds/LinkedListU.java | 0 .../src}/com/ds/LinkedListUDemo.java | 0 .../src}/com/ds/Node.java | 0 .../src}/com/ds/NodeD.java | 0 .../src}/com/ds/OneDArray.java | 0 .../src}/com/ds/QueueArray.java | 0 .../src}/com/ds/QueueArrayDemo.java | 0 .../src}/com/ds/QueueDArray.java | 0 .../src}/com/ds/QueueDArrayDemo.java | 0 .../src}/com/ds/QueueLinkedList.java | 0 .../src}/com/ds/QueueLinkedListDemo.java | 0 .../src}/com/ds/StackArray.java | 0 .../src}/com/ds/StackArrayDemo.java | 0 .../src}/com/ds/StackDArray.java | 0 .../src}/com/ds/StackDArrayDemo.java | 0 .../src}/com/ds/StackLinkedList.java | 0 .../src}/com/ds/StackLinkedListDemo.java | 0 .../src}/com/ds/Student.java | 0 .../src}/com/ds/TwoDArray.java | 0 .../src}/com/ds/hash/Entry.java | 0 .../src}/com/ds/hash/HashTableArray.java | 0 .../src}/com/ds/hash/HashTableArrayDemo.java | 0 .../src}/com/graph/BFSGraph.java | 0 .../src}/com/graph/BFSGraphDemo.java | 0 .../src}/com/graph/DFSGraph.java | 0 .../src}/com/graph/DFSGraphDemo.java | 0 .../src}/com/graph/Node.java | 0 .../src}/com/graph/adjList.java | 0 .../src}/com/search/BinarySearch.java | 0 .../src}/com/search/DataSet.java | 0 .../src}/com/search/InterploationSearch.java | 0 .../src}/com/search/LinearSearch.java | 0 .../src}/com/sort/BubbleSorting.java | 0 .../src}/com/sort/HeapSort.java | 0 .../src}/com/sort/MergeSort.java | 0 .../src}/com/sort/QuickSorting.java | 0 .../src}/com/sort/SelectionSorting.java | 0 .../src}/com/tree/BST.java | 0 .../src}/com/tree/BSTDemo.java | 0 .../src}/com/tree/Node.java | 0 bin/collection/com/HashMapClass.class | Bin 1657 -> 0 bytes bin/collection/com/HashSetClass.class | Bin 1493 -> 0 bytes bin/collection/com/LinkListArrayList.class | Bin 1367 -> 0 bytes bin/collection/com/NodeS.class | Bin 766 -> 0 bytes bin/collection/com/Student.class | Bin 805 -> 0 bytes bin/collection/com/TreeSetExample.class | Bin 1592 -> 0 bytes bin/collection/com/VectorExample.class | Bin 1860 -> 0 bytes bin/collection/com/myQueue.class | Bin 1625 -> 0 bytes bin/collection/com/myStack.class | Bin 887 -> 0 bytes bin/com/ds/DynamicArray.class | Bin 1299 -> 0 bytes bin/com/ds/DynamicArrayDemo.class | Bin 1367 -> 0 bytes bin/com/ds/LinkedListD.class | Bin 1234 -> 0 bytes bin/com/ds/LinkedListDDemo.class | Bin 946 -> 0 bytes bin/com/ds/LinkedListU.class | Bin 1171 -> 0 bytes bin/com/ds/LinkedListUDemo.class | Bin 946 -> 0 bytes bin/com/ds/Node.class | Bin 402 -> 0 bytes bin/com/ds/NodeD.class | Bin 468 -> 0 bytes bin/com/ds/OneDArray.class | Bin 1336 -> 0 bytes bin/com/ds/QueueArray.class | Bin 1635 -> 0 bytes bin/com/ds/QueueArrayDemo.class | Bin 1141 -> 0 bytes bin/com/ds/QueueDArray.class | Bin 2030 -> 0 bytes bin/com/ds/QueueDArrayDemo.class | Bin 1092 -> 0 bytes bin/com/ds/QueueLinkedList.class | Bin 1343 -> 0 bytes bin/com/ds/QueueLinkedListDemo.class | Bin 963 -> 0 bytes bin/com/ds/StackArray.class | Bin 1556 -> 0 bytes bin/com/ds/StackArrayDemo.class | Bin 1082 -> 0 bytes bin/com/ds/StackDArray.class | Bin 1943 -> 0 bytes bin/com/ds/StackDArrayDemo.class | Bin 1098 -> 0 bytes bin/com/ds/StackLinkedList.class | Bin 1184 -> 0 bytes bin/com/ds/StackLinkedListDemo.class | Bin 914 -> 0 bytes bin/com/ds/Student.class | Bin 401 -> 0 bytes bin/com/ds/TwoDArray.class | Bin 1060 -> 0 bytes bin/com/ds/hash/Entry.class | Bin 685 -> 0 bytes bin/com/ds/hash/HashTableArray.class | Bin 1483 -> 0 bytes bin/com/ds/hash/HashTableArrayDemo.class | Bin 950 -> 0 bytes bin/com/graph/BFSGraph.class | Bin 2008 -> 0 bytes bin/com/graph/BFSGraphDemo.class | Bin 647 -> 0 bytes bin/com/graph/DFSGraph.class | Bin 2115 -> 0 bytes bin/com/graph/DFSGraphDemo.class | Bin 647 -> 0 bytes bin/com/graph/Node.class | Bin 377 -> 0 bytes bin/com/graph/adjList.class | Bin 300 -> 0 bytes bin/com/search/BinarySearch.class | Bin 1453 -> 0 bytes bin/com/search/DataSet.class | Bin 543 -> 0 bytes bin/com/search/InterploationSearch.class | Bin 1506 -> 0 bytes bin/com/search/LinearSearch.class | Bin 1336 -> 0 bytes bin/com/sort/BubbleSorting.class | Bin 1344 -> 0 bytes bin/com/sort/HeapSort.class | Bin 2008 -> 0 bytes bin/com/sort/MergeSort.class | Bin 1894 -> 0 bytes bin/com/sort/QuickSorting.class | Bin 1507 -> 0 bytes bin/com/sort/SelectionSorting.class | Bin 1358 -> 0 bytes bin/com/tree/BST.class | Bin 1245 -> 0 bytes bin/com/tree/BSTDemo.class | Bin 725 -> 0 bytes bin/com/tree/Node.class | Bin 434 -> 0 bytes 135 files changed, 1407 insertions(+) create mode 100644 CodingInterviews/.classpath create mode 100644 CodingInterviews/.project create mode 100644 CodingInterviews/.settings/org.eclipse.jdt.core.prefs create mode 100644 CodingInterviews/src/com/bst/BST.java create mode 100644 CodingInterviews/src/com/bst/Node.java create mode 100644 CodingInterviews/src/com/linkedList/LinkedListManager.java create mode 100644 CodingInterviews/src/com/linkedList/LinkedListU.java create mode 100644 CodingInterviews/src/com/linkedList/ListNode.java create mode 100644 CodingInterviews/src/com/problem1/Cell.java create mode 100644 CodingInterviews/src/com/problem1/ShortPath.java create mode 100644 CodingInterviews/src/com/problem11/TreeNetwork.java create mode 100644 CodingInterviews/src/com/problem12/Mirror.java create mode 100644 CodingInterviews/src/com/problem13/GraphLevel.java create mode 100644 CodingInterviews/src/com/problem14/FirendCircle.java create mode 100644 CodingInterviews/src/com/problem15/WordDistance.java create mode 100644 CodingInterviews/src/com/problem16/WordSimilar.java create mode 100644 CodingInterviews/src/com/problem17/WordBreak.java create mode 100644 CodingInterviews/src/com/problem18/SprialMatrix.java create mode 100644 CodingInterviews/src/com/problem2/GCell.java create mode 100644 CodingInterviews/src/com/problem2/TripPlaner.java create mode 100644 CodingInterviews/src/com/problem20/TopMoviesInStream.java create mode 100644 CodingInterviews/src/com/problem21/MerageLinkedList.java create mode 100644 CodingInterviews/src/com/problem3/FindPathBetweenTwoNodes.java create mode 100644 CodingInterviews/src/com/problem4/SimilarTree.java create mode 100644 CodingInterviews/src/com/problem5/LCSOnes.java create mode 100644 CodingInterviews/src/com/problem6/BinarySearch.java create mode 100644 CodingInterviews/src/com/problem7/LCS.java create mode 100644 CodingInterviews/src/com/problem8/Stair.java create mode 100644 CodingInterviews/src/com/problem8/StairCaseDemo.java create mode 100644 CodingInterviews/src/com/problem9/Parentheses.java rename {src => DataStructureAndAlgorithms/src}/collection/com/HashMapClass.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/HashSetClass.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/LinkListArrayList.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/Student.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/TreeSetExample.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/VectorExample.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/myQueue.java (100%) rename {src => DataStructureAndAlgorithms/src}/collection/com/myStack.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/DynamicArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/DynamicArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/LinkedListD.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/LinkedListDDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/LinkedListU.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/LinkedListUDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/Node.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/NodeD.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/OneDArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueDArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueDArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueLinkedList.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/QueueLinkedListDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackDArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackDArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackLinkedList.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/StackLinkedListDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/Student.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/TwoDArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/hash/Entry.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/hash/HashTableArray.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/ds/hash/HashTableArrayDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/BFSGraph.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/BFSGraphDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/DFSGraph.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/DFSGraphDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/Node.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/graph/adjList.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/search/BinarySearch.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/search/DataSet.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/search/InterploationSearch.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/search/LinearSearch.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/sort/BubbleSorting.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/sort/HeapSort.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/sort/MergeSort.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/sort/QuickSorting.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/sort/SelectionSorting.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/tree/BST.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/tree/BSTDemo.java (100%) rename {src => DataStructureAndAlgorithms/src}/com/tree/Node.java (100%) delete mode 100644 bin/collection/com/HashMapClass.class delete mode 100644 bin/collection/com/HashSetClass.class delete mode 100644 bin/collection/com/LinkListArrayList.class delete mode 100644 bin/collection/com/NodeS.class delete mode 100644 bin/collection/com/Student.class delete mode 100644 bin/collection/com/TreeSetExample.class delete mode 100644 bin/collection/com/VectorExample.class delete mode 100644 bin/collection/com/myQueue.class delete mode 100644 bin/collection/com/myStack.class delete mode 100644 bin/com/ds/DynamicArray.class delete mode 100644 bin/com/ds/DynamicArrayDemo.class delete mode 100644 bin/com/ds/LinkedListD.class delete mode 100644 bin/com/ds/LinkedListDDemo.class delete mode 100644 bin/com/ds/LinkedListU.class delete mode 100644 bin/com/ds/LinkedListUDemo.class delete mode 100644 bin/com/ds/Node.class delete mode 100644 bin/com/ds/NodeD.class delete mode 100644 bin/com/ds/OneDArray.class delete mode 100644 bin/com/ds/QueueArray.class delete mode 100644 bin/com/ds/QueueArrayDemo.class delete mode 100644 bin/com/ds/QueueDArray.class delete mode 100644 bin/com/ds/QueueDArrayDemo.class delete mode 100644 bin/com/ds/QueueLinkedList.class delete mode 100644 bin/com/ds/QueueLinkedListDemo.class delete mode 100644 bin/com/ds/StackArray.class delete mode 100644 bin/com/ds/StackArrayDemo.class delete mode 100644 bin/com/ds/StackDArray.class delete mode 100644 bin/com/ds/StackDArrayDemo.class delete mode 100644 bin/com/ds/StackLinkedList.class delete mode 100644 bin/com/ds/StackLinkedListDemo.class delete mode 100644 bin/com/ds/Student.class delete mode 100644 bin/com/ds/TwoDArray.class delete mode 100644 bin/com/ds/hash/Entry.class delete mode 100644 bin/com/ds/hash/HashTableArray.class delete mode 100644 bin/com/ds/hash/HashTableArrayDemo.class delete mode 100644 bin/com/graph/BFSGraph.class delete mode 100644 bin/com/graph/BFSGraphDemo.class delete mode 100644 bin/com/graph/DFSGraph.class delete mode 100644 bin/com/graph/DFSGraphDemo.class delete mode 100644 bin/com/graph/Node.class delete mode 100644 bin/com/graph/adjList.class delete mode 100644 bin/com/search/BinarySearch.class delete mode 100644 bin/com/search/DataSet.class delete mode 100644 bin/com/search/InterploationSearch.class delete mode 100644 bin/com/search/LinearSearch.class delete mode 100644 bin/com/sort/BubbleSorting.class delete mode 100644 bin/com/sort/HeapSort.class delete mode 100644 bin/com/sort/MergeSort.class delete mode 100644 bin/com/sort/QuickSorting.class delete mode 100644 bin/com/sort/SelectionSorting.class delete mode 100644 bin/com/tree/BST.class delete mode 100644 bin/com/tree/BSTDemo.class delete mode 100644 bin/com/tree/Node.class diff --git a/CodingInterviews/.classpath b/CodingInterviews/.classpath new file mode 100644 index 0000000..c0f260f --- /dev/null +++ b/CodingInterviews/.classpath @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/CodingInterviews/.project b/CodingInterviews/.project new file mode 100644 index 0000000..64db0a7 --- /dev/null +++ b/CodingInterviews/.project @@ -0,0 +1,17 @@ + + + CodingInterviews + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/CodingInterviews/.settings/org.eclipse.jdt.core.prefs b/CodingInterviews/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..71f736f --- /dev/null +++ b/CodingInterviews/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,14 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=12 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=12 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=12 diff --git a/CodingInterviews/src/com/bst/BST.java b/CodingInterviews/src/com/bst/BST.java new file mode 100644 index 0000000..34f4381 --- /dev/null +++ b/CodingInterviews/src/com/bst/BST.java @@ -0,0 +1,57 @@ +package com.bst; + + +public class BST { + public Node root; + + public BST() { + root = null; + } + + /* + * Build Tree + */ + public void add (int value, Node rootClone) { + + if( root ==null) { + root = new Node(value, null, null); + return; + } + + if( value > rootClone.value) { + + if( rootClone.rightNode == null) { + rootClone.rightNode = new Node(value, null, null); + }else { + add(value, rootClone.rightNode); + } + } + + if( value < rootClone.value) { + + if( rootClone.leftNode == null) { + rootClone.leftNode = new Node(value, null, null); + }else { + add(value, rootClone.leftNode); + } + } + + + } + + + + + + + + + + + + + + + + +} diff --git a/CodingInterviews/src/com/bst/Node.java b/CodingInterviews/src/com/bst/Node.java new file mode 100644 index 0000000..6bd44cd --- /dev/null +++ b/CodingInterviews/src/com/bst/Node.java @@ -0,0 +1,13 @@ +package com.bst; + +public class Node { + public int value; + public Node leftNode; + public Node rightNode; + public Node(int value, Node leftNode, Node rightNode) { + this.value = value; + this.leftNode = leftNode ; + this.rightNode = rightNode; + } + +} diff --git a/CodingInterviews/src/com/linkedList/LinkedListManager.java b/CodingInterviews/src/com/linkedList/LinkedListManager.java new file mode 100644 index 0000000..7cd38e9 --- /dev/null +++ b/CodingInterviews/src/com/linkedList/LinkedListManager.java @@ -0,0 +1,42 @@ +package com.linkedList; + +public class LinkedListManager { + + public static void main(String[] args) { + + LinkedListU ls = new LinkedListU(); + ls.add(1); + ls.add(2); + ls.add(3); + ls.add(4); + ls.add(5); + ls.add(6); + ls.display(); + + deleteKthElement(ls.head, 2); + System.out.println("After delete"); + ls.display(); + + } + + static void deleteKthElement(ListNode root, int k) { + + //init + ListNode slowPtr = root; + ListNode fastPtr = root; + + // move fast ptr to slow + k + while(k>0) { + fastPtr = fastPtr.next; + k--; + } + + while(fastPtr.next != null) { + slowPtr = slowPtr.next; + fastPtr = fastPtr.next; + } + + slowPtr.next = slowPtr.next.next; + + } +} diff --git a/CodingInterviews/src/com/linkedList/LinkedListU.java b/CodingInterviews/src/com/linkedList/LinkedListU.java new file mode 100644 index 0000000..625c1a6 --- /dev/null +++ b/CodingInterviews/src/com/linkedList/LinkedListU.java @@ -0,0 +1,31 @@ +package com.linkedList; + +public class LinkedListU { + + ListNode head; + public LinkedListU() { + head= null; + } + public void add(Object value){ + ListNode newNode=new ListNode(value, null); + if(head==null) + head=newNode; + else{ + newNode.next=head; + head= newNode; + } + } + public void delete(){ + head=head.next; + + } + public void display(){ + ListNode n=head; + while(n!=null){ + System.out.println((T)n.value); + n=n.next; + } + } + + +} diff --git a/CodingInterviews/src/com/linkedList/ListNode.java b/CodingInterviews/src/com/linkedList/ListNode.java new file mode 100644 index 0000000..33cfacc --- /dev/null +++ b/CodingInterviews/src/com/linkedList/ListNode.java @@ -0,0 +1,12 @@ +package com.linkedList; + +public class ListNode { + + Object value; + ListNode next; + public ListNode(Object value,ListNode next) { + this.value=value; + this.next=next; + } + +} diff --git a/CodingInterviews/src/com/problem1/Cell.java b/CodingInterviews/src/com/problem1/Cell.java new file mode 100644 index 0000000..981a255 --- /dev/null +++ b/CodingInterviews/src/com/problem1/Cell.java @@ -0,0 +1,15 @@ +package com.problem1; + +public class Cell { + + public int rowIndex; + public int colIndex; + public int distance; + public Cell( int rowIndex,int colIndex,int distance) { + this.rowIndex = rowIndex; + this.colIndex = colIndex; + this.distance =distance; + + } + +} diff --git a/CodingInterviews/src/com/problem1/ShortPath.java b/CodingInterviews/src/com/problem1/ShortPath.java new file mode 100644 index 0000000..fc7af60 --- /dev/null +++ b/CodingInterviews/src/com/problem1/ShortPath.java @@ -0,0 +1,71 @@ +package com.problem1; + +import java.util.LinkedList; +import java.util.Queue; + +public class ShortPath { + + public static void main(String[] args) { + + //We give 9 for post location + int[][] grid = {{1,1,1,1,0},{0,0,0,1,0},{0,0,1,1,0}, {0,0,1,0,0},{0,0,0,9,0} }; + print(grid); + + Queue queue = new LinkedList(); + //Set robot location + move(grid,queue, 0,0, 0); + + while ( !queue.isEmpty()) { + + Cell botCell = queue.poll(); + // left + + move (grid, queue, botCell.rowIndex -1, botCell.colIndex, botCell.distance +1); + + //right + move (grid, queue, botCell.rowIndex +1, botCell.colIndex, botCell.distance +1); + + // top + + move (grid, queue, botCell.rowIndex, botCell.colIndex -1, botCell.distance +1); + + + //button + + move (grid, queue, botCell.rowIndex, botCell.colIndex +1, botCell.distance +1); + + } + System.out.println("Not path found"); + + } + + static void move( int[][] grid, Queue queue, int newRow, int newCol, int newDistance) { + + // make sure the robot in the grid + if( newRow >=0 && newRow < grid.length && newCol >= 0 && newCol < grid[0].length) { + + if ( grid[newRow][newCol] == 1) { + queue.add(new Cell(newRow, newCol, newDistance)); + grid[newRow][newCol] = -1; // cell is visited + }else if( grid[newRow][newCol] == 9) { + //path is found + System.out.println("Path is found, Distnace: "+ newDistance); + System.exit(0); + } + + } + } + + static void print( int[][] grid) { + + for(int i=0;i serializeList = new ArrayList(); + void serialize(Node node) { + + if(node != null) { + serializeList.add(node.value); + serialize(node.leftNode); + serialize(node.rightNode); + + }else { + serializeList.add(-1); + } + } + + int index =0; + Node deSerialize() { + + if( index >serializeList.size()-1 || serializeList.get(index)==-1) { + index ++; + return null; + } + + Node newNode = new Node(serializeList.get(index++), null, null); + newNode.leftNode = deSerialize() ; + newNode.rightNode = deSerialize() ; + return newNode; + } + + + + + + +} diff --git a/CodingInterviews/src/com/problem12/Mirror.java b/CodingInterviews/src/com/problem12/Mirror.java new file mode 100644 index 0000000..299fa09 --- /dev/null +++ b/CodingInterviews/src/com/problem12/Mirror.java @@ -0,0 +1,63 @@ +package com.problem12; + +import com.bst.Node; + +public class Mirror { + + + public static void main(String[] args) { + + new Mirror().buildTress(); + } + + + void buildTress() { + + // Tree 1 + Node node1 = new Node(1, null, null); + Node node2 = new Node(2, null, null); + Node node3 = new Node(3, null, null); + Node node4 = new Node(4, null, null); + Node node5 = new Node(5, null, null); + + // build Tree 1 + node1.leftNode = node3; + node1.rightNode = node2; + node2.leftNode = node5; + node2.rightNode = node4; + + explore1(node1); + + + System.out.println(); + // build Tree 2 + node1.leftNode = node2; + node1.rightNode = node3; + node2.leftNode = node4; + node2.rightNode = node5; + explore2(node1); + + } + + // TLR + void explore1(Node node) { + if( node !=null) { + System.out.print( node.value +"\t"); + explore1(node.leftNode); + explore1(node.rightNode); + } + } + + + // TRL + void explore2(Node node) { + if( node !=null) { + System.out.print( node.value +"\t"); + explore2(node.rightNode); + explore2(node.leftNode); + + } + } + + +} diff --git a/CodingInterviews/src/com/problem13/GraphLevel.java b/CodingInterviews/src/com/problem13/GraphLevel.java new file mode 100644 index 0000000..3cffb26 --- /dev/null +++ b/CodingInterviews/src/com/problem13/GraphLevel.java @@ -0,0 +1,90 @@ +package com.problem13; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +import javax.management.Query; + +import com.bst.Node; + + +public class GraphLevel { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + + // Tree 1 + Node node1 = new Node(1, null, null); + Node node2 = new Node(2, null, null); + Node node3 = new Node(3, null, null); + Node node4 = new Node(4, null, null); + Node node5 = new Node(5, null, null); + Node node6 = new Node(6, null, null); + + // build Tree 1 + node1.leftNode = node3; + node1.rightNode = node2; + node2.leftNode = node5; + node2.rightNode = node4; + node3.leftNode = node6; + + ArrayList> listOfLevels = new GraphLevel().getLevels(node1); + for (ArrayList level : listOfLevels) { + System.out.println(level); + } + + // balance + ArrayList> listOfLevels1 = new GraphLevel().getLevels(node1.leftNode); + ArrayList> listOfLevels2 = new GraphLevel().getLevels(node1.rightNode); + System.out.println(listOfLevels1.size() + "," + listOfLevels2.size()); + if(listOfLevels1.size() == listOfLevels2.size()) { + System.out.println(" Tree balance"); + }else { + System.out.println(" Tree not balance"); + } + + } + + + ArrayList> getLevels( Node node){ + + ArrayList> listOfLevels = new ArrayList>(); + + Queue nodesQueue = new LinkedList(); + nodesQueue.add(node); + int preLevel =1; + int currentLevel=0; + ArrayList levelElements = new ArrayList(); + while( !nodesQueue.isEmpty()) { + + Node newNode = nodesQueue.poll(); + levelElements.add(newNode.value); + if( newNode.leftNode != null) { + nodesQueue.add(newNode.leftNode); + currentLevel++; + } + + if( newNode.rightNode != null) { + nodesQueue.add(newNode.rightNode); + currentLevel++; + } + + preLevel --; + if(preLevel==0) { + listOfLevels.add(levelElements); + preLevel = currentLevel; + currentLevel = 0; + levelElements = new ArrayList(); + + } + } + + + + return listOfLevels; + + } + +} diff --git a/CodingInterviews/src/com/problem14/FirendCircle.java b/CodingInterviews/src/com/problem14/FirendCircle.java new file mode 100644 index 0000000..2758b78 --- /dev/null +++ b/CodingInterviews/src/com/problem14/FirendCircle.java @@ -0,0 +1,70 @@ +package com.problem14; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +public class FirendCircle { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + //input + int[][] connections = { + {1,1,0,0,0}, + {1,1,1,0,0}, + {0,1,1,0,0}, + {0,0,0,1,1}, + {0,0,0,1,1} + }; + + ArrayList> listOfCircles = new FirendCircle().getCircles(connections); + for (ArrayList circle : listOfCircles) { + System.out.println(circle); + } + } + + + ArrayList> getCircles(int[][] connections){ + + ArrayList> listOfCircles = new ArrayList>(); + + // save if person in any circle + boolean[] visited = new boolean[ connections.length]; + + for(int row=0;row< connections.length ; row++) { + + if(visited[row] == true) { + continue; + } + + visited[row] = true; + + Queue circleQueue = new LinkedList(); + + circleQueue.add(row); + ArrayList listOfFirends = new ArrayList(); + while( !circleQueue.isEmpty()) { + + int userID = circleQueue.poll(); + listOfFirends.add(userID); + + for( int userFirendsID =0;userFirendsID< connections[0].length;userFirendsID++) { + + if( connections[userID][userFirendsID]== 1 && visited[userFirendsID]!= true) { + circleQueue.add(userFirendsID); + visited[userFirendsID] = true; + } + } + + } + + listOfCircles.add(listOfFirends); + + } + + + return listOfCircles; + } + +} diff --git a/CodingInterviews/src/com/problem15/WordDistance.java b/CodingInterviews/src/com/problem15/WordDistance.java new file mode 100644 index 0000000..341b0d3 --- /dev/null +++ b/CodingInterviews/src/com/problem15/WordDistance.java @@ -0,0 +1,72 @@ +package com.problem15; + +import java.util.Arrays; +import java.util.Stack; + +public class WordDistance { + + public static void main(String[] args) { + // TODO Auto-generated method stub + + // input + String[] dic = {"hot","dot","dog","lot","log","cog"}; + String start= "hit"; + String end = "cog"; + + boolean isFound =new WordDistance().findPath(dic, start, end); + System.out.println("isFound: "+ isFound); + } + + + boolean findPath( String[] dic,String startWord, String endWord) { + + boolean isFound =false; + + boolean[] visited = new boolean[dic.length]; + + Stack stack = new Stack(); + stack.push(startWord); + + while(!stack.isEmpty()) { + + String word = stack.pop(); + if( distance( word, endWord)==0) { + stack.push(word); + System.out.println(stack); + isFound= true; + break; + } + + for(int i=0;i listOfWords = new ArrayList(); + + public Option(int nextIndex, ArrayList listOfWords) { + + this.nextIndex = nextIndex; + this.listOfWords.addAll(listOfWords); + } + + +} + +public class WordBreak { + + Set dictionary = new HashSet<>(); + + public WordBreak() { + + dictionary.add("mobile") ; + dictionary.add("samsung") ; + dictionary.add("sam") ; + dictionary.add("sung") ; + dictionary.add("man") ; + dictionary.add("mango") ; + dictionary.add("icecream") ; + dictionary.add("and") ; + dictionary.add("go") ; + dictionary.add("i") ; + dictionary.add("like"); + dictionary.add("ice") ; + dictionary.add("cream") ; + } + + public static void main(String[] args) { + // TODO Auto-generated method stub + + new WordBreak().breakWordNow("ilikesamsungandmango"); + } + + void breakWordNow(String word) { + + Queue