Skip to content

Commit 1a914a4

Browse files
authored
Merge pull request #8 from Subham1999/back_tracking
LLD for In Memor key-val store
2 parents 9cf4e24 + b8ddcbc commit 1a914a4

File tree

77 files changed

+324
-47
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+324
-47
lines changed

.idea/encodings.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 84 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

data-structures/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.thealgorithm</groupId>
8+
<artifactId>TheAlgorithm</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>com.thealgorithm</groupId>
13+
<artifactId>data-structures</artifactId>
14+
</project>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.thealgorithm.miscelleneous;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
import java.util.List;
6+
import java.util.Set;
7+
8+
/**
9+
* @author: Subham Santra
10+
*/
11+
public class GenerateParenthesis {
12+
13+
public List<String> generate(int c1, int c2, int c3) {
14+
Set<String> result = new HashSet<>();
15+
generate(c1, 0, 0, c2, 0, 0, c3, 0, 0, result, new StringBuilder());
16+
return new ArrayList<>(result);
17+
}
18+
19+
private void generate(
20+
int c1,
21+
int o1,
22+
int e1,
23+
int c2,
24+
int o2,
25+
int e2,
26+
int c3,
27+
int o3,
28+
int e3,
29+
Set<String> result,
30+
StringBuilder comb) {
31+
32+
if (e1 > o1 || e2 > o2 || e3 > o3) {
33+
return;
34+
}
35+
36+
// if (o1 == c1 && o1 == e1 && o2 == c2 && o2 == e2 && o3 == c3 && o3 == e3) {
37+
if (o1 == c1 && o2 == c2 && o3 == c3) {
38+
// if (o1 == e1 && o2 == e2 && o3 == e3) {
39+
result.add(comb.toString());
40+
return;
41+
}
42+
43+
if (o1 < c1) {
44+
comb.append('(');
45+
generate(c1, o1 + 1, e1, c2, o2, e2, c3, o3, e3, result, comb);
46+
comb.deleteCharAt(comb.length() - 1);
47+
48+
comb.append(')');
49+
generate(c1, o1, e1 + 1, c2, o2, e2, c3, o3, e3, result, comb);
50+
comb.deleteCharAt(comb.length() - 1);
51+
}
52+
53+
if (o2 < c2) {
54+
comb.append('{');
55+
generate(c1, o1, e1, c2, o2 + 1, e2, c3, o3, e3, result, comb);
56+
comb.deleteCharAt(comb.length() - 1);
57+
58+
comb.append('}');
59+
generate(c1, o1, e1, c2, o2, e2 + 1, c3, o3, e3, result, comb);
60+
comb.deleteCharAt(comb.length() - 1);
61+
}
62+
63+
if (o3 < c3) {
64+
comb.append('[');
65+
generate(c1, o1, e1, c2, o2, e2, c3, o3 + 1, e3, result, comb);
66+
comb.deleteCharAt(comb.length() - 1);
67+
68+
comb.append(']');
69+
generate(c1, o1 + 1, e1, c2, o2, e2, c3, o3, e3 + 1, result, comb);
70+
comb.deleteCharAt(comb.length() - 1);
71+
}
72+
}
73+
74+
public static void main(String[] args) {
75+
System.out.println(new GenerateParenthesis().generate(2, 2, 2));
76+
}
77+
}

src/main/java/com/thealgorithm/dp/miscelleneous/LeetCode1155.java renamed to data-structures/src/main/java/com/thealgorithm/miscelleneous/LeetCode1155.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithm.dp.miscelleneous;
1+
package com.thealgorithm.miscelleneous;
22

33
/**
44
* @see <a

src/main/java/com/thealgorithm/dp/miscelleneous/LeetCode1235.java renamed to data-structures/src/main/java/com/thealgorithm/miscelleneous/LeetCode1235.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithm.dp.miscelleneous;
1+
package com.thealgorithm.miscelleneous;
22

33
import java.util.Arrays;
44

src/main/java/com/thealgorithm/dp/miscelleneous/LeetCode1335.java renamed to data-structures/src/main/java/com/thealgorithm/miscelleneous/LeetCode1335.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithm.dp.miscelleneous;
1+
package com.thealgorithm.miscelleneous;
22

33
import java.util.Arrays;
44

src/main/java/com/thealgorithm/dp/miscelleneous/LeetCode446.java renamed to data-structures/src/main/java/com/thealgorithm/miscelleneous/LeetCode446.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.thealgorithm.dp.miscelleneous;
1+
package com.thealgorithm.miscelleneous;
22

33
import java.util.ArrayList;
44
import java.util.HashMap;

low-level-design/pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>com.thealgorithm</groupId>
8+
<artifactId>TheAlgorithm</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<groupId>com.thealgorithm</groupId>
13+
<artifactId>low-level-design</artifactId>
14+
</project>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.thealgorithm.caching;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Scanner;
5+
import java.util.StringTokenizer;
6+
7+
/**
8+
* @author: Subham Santra
9+
*/
10+
public class CachingLocalTest {
11+
public static void main(String[] args) {
12+
InMemoryKeyValStore keyValStore = InMemoryKeyValStore.create();
13+
try (Scanner scanner = new Scanner(System.in)) {
14+
15+
do {
16+
String line = scanner.nextLine();
17+
StringTokenizer tokenizer = new StringTokenizer(line);
18+
19+
String var1 = tokenizer.nextToken();
20+
String var2 = tokenizer.nextToken();
21+
String var3 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
22+
String var4 = tokenizer.hasMoreTokens() ? tokenizer.nextToken() : "";
23+
24+
try {
25+
if (var1.equalsIgnoreCase("put")) {
26+
if (var4.isBlank()) {
27+
keyValStore.put(var2, var3.getBytes(StandardCharsets.UTF_8), 60_000L);
28+
} else {
29+
keyValStore.put(var2, var3.getBytes(StandardCharsets.UTF_8), Long.parseLong(var4));
30+
}
31+
} else if (var1.equalsIgnoreCase("get")) {
32+
System.out.println(new String(keyValStore.get(var2)));
33+
} else if (var1.equalsIgnoreCase("rem")) {
34+
keyValStore.rem(var2);
35+
}
36+
} catch (DataNotPresentException | DataExpiredException e) {
37+
System.err.println(e.getMessage());
38+
}
39+
} while (scanner.hasNextLine());
40+
}
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.thealgorithm.caching;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class DataExpiredException extends Exception {
7+
public DataExpiredException(String s) {
8+
super(s);
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.thealgorithm.caching;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class DataNotPresentException extends Exception {
7+
public DataNotPresentException(String s) {
8+
super(s);
9+
}
10+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.thealgorithm.caching;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
import lombok.Value;
6+
7+
/**
8+
* @author: Subham Santra
9+
* @apiNote
10+
* <li>$ put key val
11+
* <li>$ put key val ttl_in_seconds
12+
* <li>$ get key
13+
* <li>$ rem key
14+
*/
15+
public class InMemoryKeyValStore {
16+
@Value
17+
private static class Entry {
18+
byte[] val;
19+
long created;
20+
long ttl_millis;
21+
}
22+
23+
private final Map<String, Entry> kvStore;
24+
25+
private InMemoryKeyValStore() {
26+
kvStore = new ConcurrentHashMap<>();
27+
}
28+
29+
public static InMemoryKeyValStore create() {
30+
return new InMemoryKeyValStore();
31+
}
32+
33+
void put(String key, byte[] val, long ttl) {
34+
kvStore.put(key, new Entry(val, System.currentTimeMillis(), ttl));
35+
}
36+
37+
byte[] get(String key) throws DataNotPresentException, DataExpiredException {
38+
if (!kvStore.containsKey(key)) {
39+
throw new DataNotPresentException("'key' : " + key + " do not present");
40+
}
41+
42+
Entry e = kvStore.get(key);
43+
if (System.currentTimeMillis() - e.created > e.ttl_millis) {
44+
kvStore.remove(key);
45+
throw new DataExpiredException("'key' : " + key + " expired");
46+
}
47+
48+
return e.val;
49+
}
50+
51+
boolean rem(String key) throws DataNotPresentException, DataExpiredException {
52+
if (!kvStore.containsKey(key)) {
53+
throw new DataNotPresentException("'key' : " + key + " do not present");
54+
}
55+
56+
Entry e = kvStore.get(key);
57+
kvStore.remove(key);
58+
return (System.currentTimeMillis() - e.created <= e.ttl_millis);
59+
}
60+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<groupId>com.thealgorithm</groupId>
88
<artifactId>TheAlgorithm</artifactId>
99
<version>1.0-SNAPSHOT</version>
10+
<packaging>pom</packaging>
11+
<modules>
12+
<module>data-structures</module>
13+
<module>low-level-design</module>
14+
</modules>
1015

1116
<properties>
1217
<maven.compiler.source>21</maven.compiler.source>

0 commit comments

Comments
 (0)