Skip to content

Commit 1d4086c

Browse files
committed
Added 4 design questions
1 parent 699bd2e commit 1d4086c

4 files changed

+209
-0
lines changed

Medium/Design Log Storage System.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class LogSystem {
2+
3+
List<LogEntry> logEntries;
4+
Map<String, Integer> granualityMap = new HashMap<>();
5+
String[] grans = "Year:Month:Day:Hour:Minute:Second".split(":");
6+
int[] gransIndex = {4,7,10,13,16,19};
7+
8+
public LogSystem() {
9+
logEntries = new ArrayList<>();
10+
for (int i=0; i<grans.length; i++) {
11+
granualityMap.put(grans[i], gransIndex[i]);
12+
}
13+
}
14+
15+
public void put(int id, String timestamp) {
16+
logEntries.add(new LogEntry(id, timestamp));
17+
}
18+
19+
public List<Integer> retrieve(String s, String e, String gra) {
20+
List<Integer> list = new ArrayList<>();
21+
Iterator<LogEntry> logIterator = logEntries.iterator();
22+
int substringEnd = granualityMap.get(gra);
23+
24+
String start = s.substring(0, substringEnd);
25+
String end = e.substring(0, substringEnd);
26+
27+
while (logIterator.hasNext()) {
28+
LogEntry entry = logIterator.next();
29+
String entryTimeStamp = entry.timestamp;
30+
31+
if (entryTimeStamp.substring(0, substringEnd).compareTo(start) >= 0 &&
32+
entryTimeStamp.substring(0, substringEnd).compareTo(end) <= 0) {
33+
list.add(entry.id);
34+
}
35+
}
36+
37+
return list;
38+
}
39+
}
40+
41+
class LogEntry {
42+
int id;
43+
String timestamp;
44+
45+
public LogEntry(int id, String timestamp) {
46+
this.id = id;
47+
this.timestamp = timestamp;
48+
}
49+
}
50+
51+
/**
52+
* Your LogSystem object will be instantiated and called as such:
53+
* LogSystem obj = new LogSystem();
54+
* obj.put(id,timestamp);
55+
* List<Integer> param_2 = obj.retrieve(s,e,gra);
56+
*/

Medium/Flatten 2D Vector.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Vector2D implements Iterator<Integer> {
2+
Iterator<List<Integer>> iterator;
3+
Iterator<Integer> listIterator;
4+
5+
public Vector2D(List<List<Integer>> vec2d) {
6+
if (vec2d == null || vec2d.isEmpty()) {
7+
return;
8+
}
9+
10+
iterator = vec2d.iterator();
11+
listIterator = iterator.next().iterator();
12+
checkForValidity();
13+
}
14+
15+
private void checkForValidity() {
16+
while (!listIterator.hasNext() && iterator.hasNext()) {
17+
listIterator = iterator.next().iterator();
18+
}
19+
}
20+
21+
@Override
22+
public Integer next() {
23+
int num = listIterator.next();
24+
checkForValidity();
25+
return num;
26+
}
27+
28+
@Override
29+
public boolean hasNext() {
30+
return listIterator != null && listIterator.hasNext();
31+
}
32+
}
33+
34+
/**
35+
* Your Vector2D object will be instantiated and called as such:
36+
* Vector2D i = new Vector2D(vec2d);
37+
* while (i.hasNext()) v[f()] = i.next();
38+
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* // This is the interface that allows for creating nested lists.
3+
* // You should not implement it, or speculate about its implementation
4+
* public interface NestedInteger {
5+
*
6+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
7+
* public boolean isInteger();
8+
*
9+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
10+
* // Return null if this NestedInteger holds a nested list
11+
* public Integer getInteger();
12+
*
13+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
14+
* // Return null if this NestedInteger holds a single integer
15+
* public List<NestedInteger> getList();
16+
* }
17+
*/
18+
public class NestedIterator implements Iterator<Integer> {
19+
List<Integer> list;
20+
int index;
21+
int size;
22+
public NestedIterator(List<NestedInteger> nestedList) {
23+
list = new ArrayList<>();
24+
index = 0;
25+
for (NestedInteger n : nestedList) {
26+
dfsHelper(n);
27+
}
28+
29+
size = list.size();
30+
}
31+
32+
private void dfsHelper(NestedInteger n) {
33+
if (n.isInteger()) {
34+
list.add(n.getInteger());
35+
}
36+
else {
37+
for (NestedInteger ni : n.getList()) {
38+
dfsHelper(ni);
39+
}
40+
}
41+
}
42+
43+
@Override
44+
public Integer next() {
45+
if (index < size) {
46+
return list.get(index++);
47+
}
48+
return -1;
49+
}
50+
51+
@Override
52+
public boolean hasNext() {
53+
return !(index == size);
54+
}
55+
}
56+
57+
/**
58+
* Your NestedIterator object will be instantiated and called as such:
59+
* NestedIterator i = new NestedIterator(nestedList);
60+
* while (i.hasNext()) v[f()] = i.next();
61+
*/

Medium/Zigzag Iterator.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class ZigzagIterator {
2+
3+
List<Integer> l1;
4+
List<Integer> l2;
5+
int start1;
6+
int start2;
7+
int end1;
8+
int end2;
9+
boolean flag;
10+
11+
public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
12+
l1 = v1;
13+
l2 = v2;
14+
start1 = 0;
15+
start2 = 0;
16+
end1 = l1.size();
17+
end2 = l2.size();
18+
flag = true;
19+
}
20+
21+
public int next() {
22+
if (start1 < end1 && start2 < end2) {
23+
int ans = -1;
24+
if (flag) {
25+
ans = l1.get(start1);
26+
start1++;
27+
flag = !flag;
28+
}
29+
else {
30+
ans = l2.get(start2);
31+
start2++;
32+
flag = !flag;
33+
}
34+
35+
return ans;
36+
}
37+
else if (start1 < end1) {
38+
return l1.get(start1++);
39+
}
40+
else {
41+
return l2.get(start2++);
42+
}
43+
}
44+
45+
public boolean hasNext() {
46+
return !(start1 == end1 && start2 == end2);
47+
}
48+
}
49+
50+
/**
51+
* Your ZigzagIterator object will be instantiated and called as such:
52+
* ZigzagIterator i = new ZigzagIterator(v1, v2);
53+
* while (i.hasNext()) v[f()] = i.next();
54+
*/

0 commit comments

Comments
 (0)