Uber | Phone screen | USA
Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2022-06-30Added By Anonymous
Source: Link
Company: Uber
Interview Stage: Phone screen
Location: USA
Original Post Content:
1 import java.io.*;
2import java.util.ArrayList;
3import java.util.HashSet;
4import java.util.List;
5import java.util.Set;
6
7import com.fasterxml.jackson.databind.ObjectMapper;
8import com.fasterxml.jackson.core.JsonProcessingException;
9
10/*
11Let's write a simple mark-and-sweep garbage collector.
12
131. Mark phase:
14You're given a list of all "known alive" objects, or "roots".
15i.e. static objects, objects that are currently on the stack, etc.
16
17Given these root objects, you traverse the object graph to mark the
18objects that are reachable from any of them as "alive".
19
20Any object in the heap that cannot be reached by any of these roots
21are marked as "dead" in this phase.
22
232. Sweep phase:
24Go through all objects, if they are marked as "dead", free them.
25
26
27*/
28
29/*
30
31A a = new A();
32B b = new B();
33a.Field1 = b;
34a.Field2 = new C();
35
36d = new D();
37
38
39a --> b
40|---> c
41
42d
43
44
45
46If a is root, then b and c are alive. since b is alive, d is also alive.
47
48
49class A {
50 static A SomeA;
51
52 public B myB;
53 public C myC;
54}
55
56public void someMethod() {
57 A.SomeA = new A();
58
59 B myB = new B(); (alive)
60 C myC = new C(); (alive)
61 D myD = new D(); // this should be GC'd.
62 A.SomeA.B = myB;
63 A.SomeA.B.C = myC;
64
65 myD.E = new E();
66
67 allObjects => {a, b, c, d, e}
68 roots => {a}
69 //a->a, b->a c->a
70
71 // how many heap objects were allocated?
72 => 4
73 return;
74 // SomeA is the only thing we "know" to be alive.
75 // SomeA has references to B and C fields.
76 // SomeA ==> B;
77 //. | ====> C;
78 //
79}
80/*
81class A
82{
83 static A SomeA;
84
85 public B myB;
86 public C myC;
87
88 public void someMethod() {
89 A.SomeA = new A();
90
91 B myB = new B();
92 C myC = new C();
93 D myD = new D(); // this should be GC'd.
94 A.SomeA.B = myB;
95 A.SomeA.B.C = myC;
96
97 myD.E = new E();
98 }
99}
100
101class B
102{
103
104}
105class C
106{
107
108}
109class D
110{
111
112}
113*/
114class HeapObject { // an GC-internal representation of an object that's allocated on the heap.
115 Object underlying;
116 boolean isAlive;
117 String name;
118 List<HeapObject> refs;
119
120 public HeapObject(String name) {
121 this.name = name;
122 isAlive = true;
123 refs = new ArrayList<HeapObject>();
124 }
125}
126
127
128// Main class should be named 'Solution'
129class Solution {
130
131 public static void Mark(List<HeapObject> allObjects, List<HeapObject> roots) {
132 // any objects in allObjects that cannot be reached by any of objects
133 // in roots should be marked as isAlive = false;
134
135
136
137 }
138 //object -> parent
139 public static void main(String[] args) {
140 System.out.println("Hello, World");
141
142
143 HeapObject a = new HeapObject("a");
144 HeapObject b = new HeapObject("b");
145 HeapObject c = new HeapObject("c");
146 HeapObject d = new HeapObject("d");
147 a.refs.add(b);
148 b.refs.add(c);
149
150 List<HeapObject> allObjects = new ArrayList<>();
151 List<HeapObject> roots = new ArrayList<>();
152 allObjects.add(a);
153 allObjects.add(b);
154 allObjects.add(c);
155 allObjects.add(d);
156 roots.add(a);
157
158 Mark(allObjects, roots);
159 }
160}