Skip to content

Commit b3258a2

Browse files
committed
Add solutions of 8 problems of CTCI tutorial
- A tale of two stacks - Balanced Brackets - Contacts - Davis Staircase - Fibonacci Numbers - FInd The Running Median - Is this a binary search tree - Lonely Integer
1 parent bd51949 commit b3258a2

8 files changed

Lines changed: 305 additions & 0 deletions
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class ATaleOfTwoStacks {
5+
6+
public static class MyQueue<T> {
7+
Stack<T> stackNewestOnTop = new Stack<T>();
8+
Stack<T> stackOldestOnTop = new Stack<T>();
9+
10+
public void enqueue(T value) { // Push onto newest stack
11+
stackNewestOnTop.add(value);
12+
}
13+
14+
public T peek() {
15+
if(!stackOldestOnTop.isEmpty()) return stackOldestOnTop.peek();
16+
while(!stackNewestOnTop.isEmpty()){
17+
stackOldestOnTop.add(stackNewestOnTop.pop());
18+
}
19+
return stackOldestOnTop.peek();
20+
}
21+
22+
public T dequeue() {
23+
if(!stackOldestOnTop.isEmpty()) return stackOldestOnTop.pop();
24+
while(!stackNewestOnTop.isEmpty()){
25+
stackOldestOnTop.add(stackNewestOnTop.pop());
26+
}
27+
return stackOldestOnTop.pop();
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
MyQueue<Integer> queue = new MyQueue<Integer>();
33+
34+
Scanner scan = new Scanner(System.in);
35+
int n = scan.nextInt();
36+
37+
for (int i = 0; i < n; i++) {
38+
int operation = scan.nextInt();
39+
if (operation == 1) { // enqueue
40+
queue.enqueue(scan.nextInt());
41+
} else if (operation == 2) { // dequeue
42+
queue.dequeue();
43+
} else if (operation == 3) { // print/peek
44+
System.out.println(queue.peek());
45+
}
46+
}
47+
scan.close();
48+
}
49+
50+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class BalancedBrackets {
5+
6+
public static boolean isBalanced(String expression) {
7+
Stack<Character> stack = new Stack<>();
8+
for (char curr : expression.toCharArray()) {
9+
if (curr == '{' || curr == '(' || curr == '[')
10+
stack.push(curr);
11+
else {
12+
if (stack.isEmpty())
13+
return false;
14+
char last = (char) stack.pop();
15+
if ((curr == '}' && last != '{') || (curr == ']' && last != '[') || (curr == ')' && last != '('))
16+
return false;
17+
}
18+
}
19+
return stack.isEmpty();
20+
}
21+
22+
public static void main(String[] args) {
23+
Scanner sc = new Scanner(System.in);
24+
int t = Integer.parseInt(sc.nextLine());
25+
26+
StringBuilder sb = new StringBuilder();
27+
28+
for (int i = 0; i < t; i++) {
29+
String expression = sc.nextLine();
30+
sb.append(isBalanced(expression) ? "YES\n" : "NO\n");
31+
}
32+
33+
System.out.println(sb.toString());
34+
35+
sc.close();
36+
}
37+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Contacts {
5+
6+
static class Trie {
7+
int count;
8+
Trie[] children;
9+
10+
public Trie() {
11+
children = new Trie[26];
12+
}
13+
14+
void addWord(String word) {
15+
count++;
16+
if(word.isEmpty()) return;
17+
char first = word.charAt(0);
18+
if(children[first-'a'] == null){
19+
children[first-'a'] = new Trie();
20+
}
21+
children[first-'a'].addWord(word.substring(1));
22+
}
23+
24+
int findWord(String word) {
25+
if(word.isEmpty()) return count;
26+
char first = word.charAt(0);
27+
if(children[first-'a'] == null) return 0;
28+
return children[first-'a'].findWord(word.substring(1));
29+
}
30+
}
31+
32+
public static void main(String[] args) {
33+
Scanner sc = new Scanner(System.in);
34+
int n = Integer.parseInt(sc.nextLine());
35+
36+
Trie trie = new Trie();
37+
StringBuilder res = new StringBuilder();
38+
39+
String[] opContact = new String[2];
40+
String op, contact;
41+
int count;
42+
for (int nItr = 0; nItr < n; nItr++) {
43+
opContact = sc.nextLine().split(" ");
44+
op = opContact[0];
45+
contact = opContact[1];
46+
47+
if (op.equals("add")) {
48+
trie.addWord(contact);
49+
} else {
50+
count = trie.findWord(contact);
51+
res.append(count + "\n");
52+
}
53+
}
54+
55+
System.out.println(res.toString());
56+
57+
sc.close();
58+
}
59+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class DavisStaircase {
5+
6+
static int[] res;
7+
static int mod = 1000000007;
8+
9+
//Complete the stepPerms function below.
10+
public static int stepPerms(int n) {
11+
if(n == 0) return 1;
12+
if(n < 0) return 0;
13+
if(res[n] == -1) {
14+
int sum = stepPerms(n-3) + stepPerms(n-2) + stepPerms(n-1);
15+
res[n] = sum;
16+
}
17+
return res[n];
18+
}
19+
20+
public static void stepPerms2(int n) {
21+
res[1] = 1;
22+
res[2] = 2;
23+
res[3] = 4;
24+
for(int i=4;i<=n;i++){
25+
res[i] = res[i-1] + res[i-2] + res[i-3];
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
res = new int[37];
31+
for(int i=0;i<37;i++) res[i] = -1;
32+
stepPerms(36);
33+
34+
Scanner sc = new Scanner(System.in);
35+
int n = sc.nextInt();
36+
int num;
37+
for (int i = 0; i < n; i++) {
38+
num = sc.nextInt();
39+
System.out.println(res[num]);
40+
}
41+
42+
sc.close();
43+
}
44+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.*;
2+
3+
public class FibonacciNumbers {
4+
5+
public static int fibonacci(int n) {
6+
int a = 0, b = 1, c = 1;
7+
if (n == 0)
8+
return a;
9+
else if (n == 1)
10+
return b;
11+
for (int i = 1; i < n; i++) {
12+
c = a + b;
13+
a = b;
14+
b = c;
15+
}
16+
return c;
17+
}
18+
19+
public static void main(String[] args) {
20+
Scanner scanner = new Scanner(System.in);
21+
int n = scanner.nextInt();
22+
scanner.close();
23+
System.out.println(fibonacci(n));
24+
}
25+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.*;
2+
import java.text.DecimalFormat;
3+
import java.util.*;
4+
5+
public class FindTheRunningMedian {
6+
7+
public static void main(String[] args) {
8+
Scanner sc = new Scanner(System.in);
9+
int n = Integer.parseInt(sc.nextLine());
10+
11+
PriorityQueue<Double> small = new PriorityQueue<>(1, Collections.reverseOrder());
12+
PriorityQueue<Double> large = new PriorityQueue<>(1);
13+
14+
StringBuilder sb = new StringBuilder();
15+
double num, ans;
16+
DecimalFormat df = new DecimalFormat("#.0");
17+
for (int i = 0; i < n; i++) {
18+
num = Integer.parseInt(sc.nextLine());
19+
if(small.isEmpty() || (num < small.peek())) small.add(num);
20+
else if(large.isEmpty() || (num > large.peek())) large.add(num);
21+
else small.add(num);
22+
23+
while(small.size() > large.size()+1) {
24+
large.add(small.poll());
25+
}
26+
27+
while(large.size() > small.size()) {
28+
small.add(large.poll());
29+
}
30+
31+
if(small.size() == large.size()) ans = (small.peek() + large.peek())/2;
32+
else if(small.size() < large.size()) ans = large.peek();
33+
else ans = small.peek();
34+
35+
sb.append(df.format(ans)+"\n");
36+
}
37+
38+
System.out.println(sb.toString());
39+
40+
sc.close();
41+
}
42+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions.
2+
3+
The Node class is defined as follows:
4+
class Node {
5+
int data;
6+
Node left;
7+
Node right;
8+
}
9+
*/
10+
11+
boolean checkBSTUtil(Node root, int max, int min) {
12+
if (root == null) return true;
13+
if (root.data < max && root.data > min) return checkBSTUtil(root.left, root.data, min) && checkBSTUtil(root.right, max, root.data);
14+
return false;
15+
}
16+
17+
boolean checkBST(Node root) {
18+
return checkBSTUtil(root, 10001, -1);
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class LonelyInteger {
5+
6+
// Complete the findLonely function below.
7+
static int findLonely(int[] arr) {
8+
int xor = arr[0];
9+
for(int i = 1; i < arr.length; i++){
10+
xor = xor ^ arr[i];
11+
}
12+
return xor;
13+
}
14+
15+
public static void main(String[] args) {
16+
Scanner sc = new Scanner(System.in);
17+
int n = Integer.parseInt(sc.nextLine());
18+
String[] arrItems = sc.nextLine().split(" ");
19+
int[] arr = new int[n];
20+
for (int i = 0; i < n; i++) {
21+
arr[i] = Integer.parseInt(arrItems[i]);
22+
}
23+
24+
int res = findLonely(arr);
25+
System.out.println(res);
26+
27+
sc.close();
28+
}
29+
}

0 commit comments

Comments
 (0)