I have been asked 2 DS/A question in Google up until now, and I failed to solve them horribly 😂
But I wanted to contribute back to LC for helping me do the minimal prepration that I did 😅
So I thought I'd share the questions here for the community.
Q1:
We are given a directed graph with node
data class Node(
val value: Int,
val outEdges: List<Node>
)We have to implement
fun serialize(node: Node): List<Int> // represents the entire graph reachable from node
fun deserialize(serializedList : List<Int>): Node // copy of the original graphThe serialize() function takes a node and returns a List<Int> representation of the graph.
The deserialize() function takes that list and reconstructs the original graph.
Constrains:
values in different nodes.Q2:
Imagine building a scrolling screenshot feature. You’re given two screenshots—s1 and s2. You need to merge them into one, by trimming out the overlapping pixels.
We have to implement
fun mergeScreenshots(
s1: Array<IntArray>,
s2: Array<IntArray>
): Array<IntArray>s1 is always the first screenshot, and s2 is the one taken after.Test case
input:
s1 = [[1,2,3,4],
[4,5,6,7],
[7,8,9,0],
[0,1,2,3]]
s2 = [[7,8,9,0],
[0,1,2,3],
[3,4,5,6],
[6,7,8,9]]
output:
[[1,2,3,4],
[4,5,6,7],
[7,8,9,0],
[0,1,2,3],
[3,4,5,6],
[6,7,8,9]]There are few edge cases in these question which you will also have to keep in mind.
If similar questions are already present on leet code, please share the links of it here so that I can try to solve them now.