Skip to content

Commit fa5d344

Browse files
committed
A couple of new solutions
1 parent e371f9c commit fa5d344

File tree

11 files changed

+211
-65
lines changed

11 files changed

+211
-65
lines changed

Java/Advanced-Java/src/Callbacks/Lambdas.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ public static void main(String[] args) {
1515
}
1616

1717
static class Service {
18-
public Consumer<String> blah = s -> {
19-
System.out.println(s);
20-
};
18+
public Consumer<String> blah = System.out::println;
2119
}
2220
}

System-design/intro.md

+62-61
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,68 @@
1-
A - Ask good questions
2-
B - Don't use buzzwords
3-
C - Clear and organized thinking
4-
D - Drive discussions with 80-20 rule
1+
## System Design Tips
2+
- A - Ask good questions
3+
- B - Don't use buzzwords
4+
- C - Clear and organized thinking
5+
- D - Drive discussions with 80-20 rule
56

67
## Things to consider
7-
Features
8-
API
9-
Availability
10-
Latency
11-
Scalability
12-
Durability
13-
Class Diagram
14-
Security and Privacy
15-
Cost-effective
8+
- Features
9+
- API
10+
- Availability
11+
- Latency
12+
- Scalability
13+
- Durability
14+
- Class Diagram
15+
- Security and Privacy
16+
- Cost-effective
1617

1718
## Concepts to know
18-
Vertical vs horizontal scaling
19-
CAP theorem
20-
ACID vs BASE
21-
Partitioning/Sharding
22-
Consistent Hashing
23-
Optimistic vs pessimistic locking
24-
Strong vs eventual consistency
25-
RelationalDB vs NoSQL
26-
Types of NoSQL
27-
Key value
28-
Wide column
29-
Document-based
30-
Graph-based
31-
Caching
32-
Data center/racks/hosts
33-
CPU/memory/Hard drives/Network bandwidth
34-
Random vs sequential read/writes to disk
35-
HTTP vs http2 vs WebSocket
36-
TCP/IP model
37-
ipv4 vs ipv6
38-
TCP vs UDP
39-
DNS lookup
40-
Http & TLS
41-
Public key infrastructure and certificate authority(CA)
42-
Symmetric vs asymmetric encryption
43-
Load Balancer
44-
CDNs & Edges
45-
Bloom filters and Count-Min sketch
46-
Paxos
47-
Leader election
48-
Design patterns and Object-oriented design
49-
Virtual machines and containers
50-
Pub-sub architecture
51-
MapReduce
52-
Multithreading, locks, synchronization, CAS(compare and set)
19+
- Vertical vs horizontal scaling
20+
- CAP theorem
21+
- ACID vs BASE
22+
- Partitioning/Sharding
23+
- Consistent Hashing
24+
- Optimistic vs pessimistic locking
25+
- Strong vs eventual consistency
26+
- RelationalDB vs NoSQL
27+
- Types of NoSQL
28+
- Key value
29+
- Wide column
30+
- Document-based
31+
- Graph-based
32+
- Caching
33+
- Data center/racks/hosts
34+
- CPU/memory/Hard drives/Network bandwidth
35+
- Random vs sequential read/writes to disk
36+
- HTTP vs http2 vs WebSocket
37+
- TCP/IP model
38+
- ipv4 vs ipv6
39+
- TCP vs UDP
40+
- DNS lookup
41+
- Http & TLS
42+
- Public key infrastructure and certificate authority(CA)
43+
- Symmetric vs asymmetric encryption
44+
- Load Balancer
45+
- CDNs & Edges
46+
- Bloom filters and Count-Min sketch
47+
- Paxos
48+
- Leader election
49+
- Design patterns and Object-oriented design
50+
- Virtual machines and containers
51+
- Pub-sub architecture
52+
- MapReduce
53+
- Multithreading, locks, synchronization, CAS(compare and set)
5354

5455
##Tools
55-
Cassandra
56-
MongoDB/Couchbase
57-
Mysql
58-
Memcached
59-
Redis
60-
Zookeeper
61-
Kafka
62-
NGINX
63-
HAProxy
64-
Solr, Elastic search
65-
Amazon S3
66-
Docker, Kubernetes, Mesos
67-
Hadoop/Spark and HDFS
56+
- Cassandra
57+
- MongoDB/Couchbase
58+
- Mysql
59+
- Memcached
60+
- Redis
61+
- Zookeeper
62+
- Kafka
63+
- NGINX
64+
- HAProxy
65+
- Solr, Elastic search
66+
- Amazon S3
67+
- Docker, Kubernetes, Mesos
68+
- Hadoop/Spark and HDFS
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

problems/Solution.java

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Solution {
5+
6+
public List<Integer> cellCompute(int[] states, int days){
7+
List<Integer> list = new ArrayList<>();
8+
9+
int[] tempStates = new int[8];
10+
int j = 0;
11+
12+
while (j < days){
13+
tempStates[0] = states[1];
14+
15+
for (int i = 1; i < 7; i++) {
16+
tempStates[i] = states[i-1] ^ states[i+1];
17+
}
18+
19+
tempStates[7] = states[6];
20+
21+
System.arraycopy(tempStates, 0, states, 0, 8);
22+
23+
j++;
24+
}
25+
26+
for (int i = 0; i < 8; i++) {
27+
list.add(states[i]);
28+
}
29+
30+
return list;
31+
}
32+
33+
public static void main(String[] args) {
34+
Solution s = new Solution();
35+
36+
System.out.println(s.cellCompute(new int[] {1,0,0,0,0,1,0,0}, 1));
37+
System.out.println(s.cellCompute(new int[] {1,1,1,0,1,1,1,1}, 2));
38+
}
39+
}

problems/Solution1.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.*;
2+
3+
public class Solution1 {
4+
5+
public List<String> sortJunctionBoxes(List<String> boxList) {
6+
List<String> list = new ArrayList<>();
7+
List<String> oldList = new ArrayList<>();
8+
List<String> newList = new ArrayList<>();
9+
10+
for (String box: boxList) {
11+
String[] boxWords = box.split(" ");
12+
13+
if (Character.isDigit(boxWords[1].charAt(0)))
14+
// if (isNumeric())
15+
newList.add(box);
16+
else
17+
oldList.add(box);
18+
}
19+
20+
// newList.forEach(System.out::println);
21+
22+
oldList.sort((o1, o2) -> {
23+
String s1 = o1.substring(o1.indexOf(" "));
24+
String s2 = o2.substring(o2.indexOf(" "));
25+
26+
if (s1.equals(s2))
27+
return o1.substring(0, o1.indexOf(" ")).compareTo(o2.substring(0, o2.indexOf(" ")));
28+
29+
return s1.compareTo(s2);
30+
});
31+
32+
list.addAll(oldList);
33+
list.addAll(newList);
34+
35+
return list;
36+
}
37+
38+
boolean isNumeric(String str){
39+
return str.contains("+d");
40+
}
41+
public static void main(String[] args) {
42+
Solution1 s = new Solution1();
43+
44+
List<String> list = new ArrayList<>();
45+
list.add("r1 box ape bit");
46+
list.add("br8 eat nim did");
47+
list.add("b4 tye 121 432");
48+
list.add("b4 sea 121 432");
49+
list.add("w1 has uni gry");
50+
list.add("b4 xi me nu");
51+
list.add("b4 32 43 54");
52+
53+
s.sortJunctionBoxes(list).forEach(System.out::println);
54+
}
55+
}

problems/Solution2.java

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import java.util.ArrayList;
2+
import java.util.HashMap;
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
public class Solution2 {
7+
8+
public List<PairInt> optimizeUtilization(List<PairInt> foregroundAppsList, List<PairInt> backgroundAppsList) {
9+
List<PairInt> list = new ArrayList<>();
10+
11+
Map<Integer, List<PairInt>> map = new HashMap<>();
12+
13+
for (PairInt pairInt : foregroundAppsList) {
14+
for (PairInt anInt : backgroundAppsList) {
15+
int f2 = pairInt.second;
16+
int b2 = anInt.second;
17+
List<PairInt> sums = map.getOrDefault(f2 + b2, new ArrayList<>());
18+
sums.add(new PairInt(pairInt.first, anInt.first));
19+
map.put(f2 + b2, sums);
20+
}
21+
}
22+
23+
map.keySet().forEach(System.out::println);
24+
25+
return list;
26+
}
27+
28+
public static void main(String[] args) {
29+
Solution2 s = new Solution2();
30+
31+
List<PairInt> foregroundAppsList = new ArrayList<>();
32+
foregroundAppsList.add(new PairInt(1,2));
33+
foregroundAppsList.add(new PairInt(2,4));
34+
foregroundAppsList.add(new PairInt(3,5));
35+
36+
List<PairInt> backgroundAppsList = new ArrayList<>();
37+
backgroundAppsList.add(new PairInt(1,3));
38+
backgroundAppsList.add(new PairInt(2,5));
39+
40+
System.out.println(s.optimizeUtilization(foregroundAppsList, backgroundAppsList));
41+
}
42+
43+
static class PairInt {
44+
45+
int first;
46+
int second;
47+
48+
PairInt(int first, int second){
49+
this.first = first;
50+
this.second = second;
51+
}
52+
}
53+
}

problems/topKFrequent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static List<String> topKFrequent(String[] words, int k){
1515
}
1616

1717
PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(
18-
(a,b) -> a.getValue() == b.getValue()
18+
(a,b) -> a.getValue().equals(b.getValue())
1919
? b.getKey().compareTo(a.getKey())
2020
: a.getValue() - b.getValue()
2121
);

0 commit comments

Comments
 (0)