Skip to content

Commit 31b9039

Browse files
committed
Blackbuck interview Questions
1 parent 3477e4a commit 31b9039

14 files changed

+384
-88
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package DSA.Arrays;
2+
3+
import java.util.Arrays;
4+
5+
public class KthLargestElement {
6+
public static void main(String[] args) {
7+
int[] arr = {5,2, 67, 34, 76, 345};
8+
int k=4;
9+
10+
System.out.println("Kth largest element: "+ kthLargest(arr, k));
11+
System.out.println(Arrays.toString(heap));
12+
}
13+
14+
private static int kthLargest(int[] arr, int k) {
15+
initHeap(k);
16+
for(int i=0;i<k;i++){
17+
heap[i] = arr[i];
18+
}
19+
for(int i=k;i<arr.length;i++){
20+
if(arr[i]>heap[0]) {
21+
heap[0] = arr[i];
22+
heapify(0);
23+
}
24+
}
25+
return heap[0];
26+
}
27+
private static int[] heap;
28+
private static int heapSize;
29+
private static int left(int i){return (2*i)+1;}
30+
private static int right(int i){return (2*i)+2;}
31+
private static int parent(int i){return (i-1)/2;}
32+
33+
private static void initHeap(int size){
34+
heapSize = size;
35+
heap = new int[heapSize];
36+
}
37+
38+
private static void heapify(int i){
39+
int l = left(i);
40+
int r = right(i);
41+
int smallest = i;
42+
if(l<heapSize && heap[l]<heap[smallest])
43+
smallest=l;
44+
if(r<heapSize && heap[r]<heap[smallest])
45+
smallest=r;
46+
47+
if (smallest!=i){
48+
swap(smallest, i);
49+
heapify(smallest);
50+
}
51+
}
52+
53+
private static void swap(int smallest, int i) {
54+
int temp = heap[smallest];
55+
heap[smallest] = heap[i];
56+
heap[i] = temp;
57+
}
58+
59+
private static int extractMin(){
60+
int min = heap[0];
61+
heap[0] = heap[heapSize-1];
62+
heapSize--;
63+
heapify(0);
64+
return min;
65+
}
66+
67+
}

src/DSA/Graphs/CityAttractions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ static class Edge{
2727
//edges = edgeTime.length;
2828
adjacencyList = new LinkedList[v];
2929
timeMap = new HashMap<>();
30-
/*edgeList = new LinkedList[edges];
30+
/*edgeList = new ReverseLinkedList[edges];
3131
for(int i=0;i<edges;i++){
32-
edgeList[i] = new LinkedList<>();
32+
edgeList[i] = new ReverseLinkedList<>();
3333
}*/
3434
this.weight = weight;
3535
//this.edgeTime = edgeTime;

src/DSA/Graphs/DetectCycle.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public class DetectCycle {
88
static class Graph {
99
int v;
1010
int e;
11-
//LinkedList<Edges>[] adjList;
11+
//ReverseLinkedList<Edges>[] adjList;
1212
Edges[] edges;
1313

1414
Graph(int v, int e) {
1515
this.v = v;
1616
this.e = e;
17-
/*adjList = new LinkedList[v];
17+
/*adjList = new ReverseLinkedList[v];
1818
for(int i=0;i<v;i++){
19-
adjList[i] = new LinkedList<>();
19+
adjList[i] = new ReverseLinkedList<>();
2020
}*/
2121
edges = new Edges[this.e];
2222
}

src/DSA/Graphs/kCores.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private void dfsUtil(int s, boolean[] visited) {
119119
}
120120
}
121121
122-
*//*int size(LinkedList<Integer> ll){
122+
*//*int size(ReverseLinkedList<Integer> ll){
123123
int size=0;
124124
Iterator it = ll.listIterator();
125125
while (it.hasNext()){
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package DSA.LinkedList;
2+
3+
public class DeleteCurrentNode {
4+
5+
public static void main(String[] args) {
6+
node head = new node(1);
7+
addNext(head,2);
8+
addNext(head,3);
9+
addNext(head,4);
10+
addNext(head,5);
11+
addNext(head,6);
12+
addNext(head,7);
13+
addNext(head,8);
14+
addNext(head,9);
15+
node toBeDeleted = head.next.next.next;
16+
System.out.println("Before conversion: ");
17+
printLL(head);
18+
System.out.println("");
19+
convert(toBeDeleted);
20+
System.out.println("After conversion: ");
21+
printLL(head);
22+
23+
}
24+
25+
private static void convert(node toBeDeleted) {
26+
if(toBeDeleted==null)
27+
return;
28+
if (toBeDeleted.next==null)
29+
toBeDeleted =null;
30+
node temp = toBeDeleted.next;
31+
toBeDeleted.next = toBeDeleted.next.next;
32+
toBeDeleted.data = temp.data;
33+
}
34+
35+
static class node{
36+
int data;
37+
node next;
38+
39+
public node(int data) {
40+
this.data = data;
41+
next = null;
42+
}
43+
}
44+
45+
static void printLL(node head){
46+
while(head!=null){
47+
System.out.print(head.data+" ");
48+
head = head.next;
49+
}
50+
}
51+
52+
static void addNext(node head, int data){
53+
if(head ==null)
54+
head = new node(data);
55+
while(head.next!=null){
56+
head = head.next;
57+
}
58+
head.next = new node(data);
59+
}
60+
61+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package DSA.LinkedList;
2+
3+
public class FirstLastLinkedList {
4+
public static void main(String[] args) {
5+
node node = new node(1);
6+
addNext(node,2);
7+
addNext(node,3);
8+
addNext(node,4);
9+
addNext(node,5);
10+
addNext(node,6);
11+
addNext(node,7);
12+
addNext(node,8);
13+
addNext(node,9);
14+
System.out.println("Before conversion: ");
15+
printLL(node);
16+
System.out.println("");
17+
convert(node);
18+
System.out.println("After conversion: ");
19+
printLL(node);
20+
21+
}
22+
23+
static class node{
24+
int data;
25+
node next;
26+
27+
public node(int data) {
28+
this.data = data;
29+
next = null;
30+
}
31+
}
32+
33+
static void printLL(node head){
34+
while(head!=null){
35+
System.out.print(head.data+" ");
36+
head = head.next;
37+
}
38+
}
39+
40+
static void addNext(node head, int data){
41+
if(head ==null)
42+
head = new node(data);
43+
while(head.next!=null){
44+
head = head.next;
45+
}
46+
head.next = new node(data);
47+
}
48+
49+
static void convert(node head){
50+
if(head==null || head.next==null)
51+
return;
52+
node mid = getMiddle(head);
53+
node head2 = mid.next;
54+
mid.next = null;
55+
56+
head2 = reverseLL(head2);
57+
node head1 = head;
58+
while(head2!=null){
59+
node temp = head1.next;
60+
head1.next = head2;
61+
head2 = head2.next;
62+
head1.next.next = temp;
63+
head1= head1.next.next;
64+
}
65+
66+
}
67+
68+
static node getMiddle(node head){
69+
if(head.next.next==null)
70+
return head.next;
71+
node fast_ptr = head, slow_ptr = head;
72+
while(fast_ptr.next!=null && fast_ptr.next.next!=null){
73+
fast_ptr=fast_ptr.next.next;
74+
slow_ptr = slow_ptr.next;
75+
}
76+
77+
return slow_ptr;
78+
}
79+
80+
static node reverseLL(node head){
81+
node prev=null, current = head, next;
82+
83+
while(current!=null){
84+
next = current.next;
85+
current.next = prev;
86+
prev = current;
87+
current = next;
88+
}
89+
return prev;
90+
}
91+
}

src/DSA/LinkedList/MergeLists.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,3 @@ Node recMergeSorted(Node head1, Node head2){
138138

139139
}
140140

141-
142-
/*This is a function problem.You only need to complete the function given below*/
143-
/*
144-
Merge two linked lists
145-
head pointer input could be NULL as well for empty list
146-
Node is defined as
147-
class Node
148-
{
149-
int data;
150-
Node next;
151-
Node(int d) {data = d; next = null; }
152-
}
153-
*/

src/DSA/LinkedList/MergeSort.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ private static Node middleElement(Node head) {
6969
return slowptr;
7070
}
7171

72+
private static Node midElement(Node head){
73+
if(head==null)
74+
return null;
75+
Node slow=head, fast=head;
76+
while(fast.next!=null){
77+
slow = slow.next;
78+
fast = fast.next.next;
79+
}
80+
return slow;
81+
}
82+
7283
static void printList(Node head) {
7384
if (head == null)
7485
System.out.println("List is empty");
@@ -87,7 +98,7 @@ public static void main(String[] args) {
8798
node = mergeSort(node);
8899
printList(node);
89100

90-
System.out.println("");
101+
//System.out.println(midElement(node).data);
91102

92103
Set<Node> nodeSet= new HashSet<>();
93104

0 commit comments

Comments
 (0)