Interview Transcript
Red Maelstrom: Hi, can you hear me?
Electric Possum: Yes, I can.
Red Maelstrom: Hello? Yeah, cool. Awesome. We'll be spending like 45 minutes on coding problem.
Electric Possum: I probably should mention. Sorry, it's my first interview at the interviewing.io. Sorry if I would be a bit more slower then. Thank you.
Red Maelstrom: Okay, cool. Awesome. So this is a problem we will try to solve. So let's say that you have a directed graph, right? And you have to find the length of the longest path from each node.
Electric Possum: So given a directed graph, find the length of the longest path from each node. Am I understanding? Right, we have a graph, so we have an amount of nodes interconnected within each other, right? Right. And our starting point is some indication of a starting node, right?
Red Maelstrom: No. For sake of this problem, what we want is considering every node as a starting point. What is the longest path from that node?
Electric Possum: What is the longest path from that node to the leaf?
Red Maelstrom: No, to any node. Let's say that this is the graph. Okay. And then the output should be like length of a longest path, which is starting from two is two, from zero is two. Length of a longest path starting from one is one because you can go from one to two only from zero you can go to zero, to one and one to two. The longest you can travel from zero is of length two, and from two there is no outgoing edge. So there is no path starting from two. So for two it would be zero, and from three you can go to two, so it would be one for three. But let's assume that we had something like this also. Then instead of one, it would have been two because the longest path is like from three is going to four and from four to five.
Electric Possum: I see. Yeah. Thanks for confirming it. So from the zero we can go to the one, to the two. From the one we can go only to the two, from the two we can go nowhere. From the three we can go to the two, to the four and to the five, and from the one. I see. So I probably should also ask you, are we searching for any language implementation or we searching in generic algorithmic?
Red Maelstrom: Yeah. So usually typically in coding interview you are supposed to explain your approach. After sharing your approach, you are supposed to analyze the time and a space complexity for your approach in terms of size of an input. And then you are supposed to implement it in any one of the language which you are familiar with. And that code need to be close to production, like working code. But you don't need to worry about getting an input from a user or anything. You just write a function assuming that input is provided as a parameter.
Electric Possum: Okay, I see.
Red Maelstrom: So for this example, what language you.
Electric Possum: Are familiar with, I would define to use Python if possible.
Red Maelstrom: Python, right? Yeah. So basically you are supposed to write a function, let's say that this longest path which will take as an input like adjacent list and the number of nodes in the graph and return at the end of result return the array or a list indicating these values as output. Okay.
Electric Possum: I think the only part that is left for me to start solving actual problem is to understand like if we have something like this edge list and this edge list is. Sorry, I need to turn off my Grammarly because it disturbing me and it's like 01234 and five how is relationship between them are set up?
Red Maelstrom: Okay. No, it wouldn't be like this. So basically it is a list of list. Okay, so every index indicates the neighbors from that node. So from zero there is only one neighbor, which is one. Right. So at the zero index list would be just one node, one. From one there is only one neighbor because you can go from one to two only. Right. So that's why the neighbor of one is two. Adjacent list is for one is just of a single node, two. From two there is nothing. So it would be an empty. From three you can go to either two or you can go to either four. So it would be two, comma four for three. And then from four you can go to just five. Right. And from five there is no node going edge going out. It would been empty for five.
Electric Possum: Yeah, now I got it. Okay. Yeah, I think I'm just changing the problem better now. Yeah, give me a second to think of how to take your time. Okay, I start to composting sub I think here what we might want to do is since we have edge list we can, we can make something like we can at the, at the first, at the first step we initialize some list of the size of the edge list with a number of zeros. So zeros is least possible distance that might be for the given node. I think it would be. I I'm thinking if there is an option to use depth first search here, because it's looking like this problem.
Red Maelstrom: I think. Yeah, depth first search is the right approach to think about, but yeah, think about how you would approach. So. But yeah, you are right. So it's something similar. Search is the right way to look at it.
Electric Possum: I think we want to create here something like, we want to first make the notes here in something like, let's say correct order. And then we could just go through the list and each time adjust the numbers accordingly, basically increment them, I think.
Red Maelstrom: Okay, so are you talking about topological sort?
Electric Possum: Yeah, I forgot the term. It's topological sort as far as I remember. Don't hold me too much to it. We can make topological sort by implementing the depth first search, right?
Red Maelstrom: Mmhmm.
Electric Possum: And go through each node.
Red Maelstrom: And use.
Electric Possum: Depth or search into each node. And this will give us a reversed topological sort graph. Right. I need to get the whole idea. And if it would be possible, what would be the next step? 1 second. So first we are finding the topological sorted tree, not tree, but graph. And then. And then, yeah, and then we, we iterating through the whole graph, through the whole sorted graph and checking the distance between node and their neighbor. And if. Yeah, and based on it, we can increment the distance for the given node. So it would look something like. I'll try to drop something here. Like we first we need to init distance, list distances, list further num of nodes. So, yeah, then we need to create a sorted view of a graph and then it like for each node to check if the longest path for the node can be incremented by current node. It. And I think that's it. This is the three steps we need to create. But for the topological sort, it's needed to what it to make DFS for each node and then to reverse it. I think this should work somehow. Like this. Is it looking promising or not? How do you think?
Red Maelstrom: Yeah, so basically what I'm saying is, if you are applying for DFS for each node, you don't even need to do a topological sort. You can just start applying dfs till you can and find the length of a longest path for that particular node.
Electric Possum: Sorry, could you please repeat it?
Red Maelstrom: What I'm saying is, if you're applying a DFS for each node, right, you don't need to worry about applying them in a topological order or anything.
Electric Possum: Yeah, you are right. I thought that the idea of topological sort is to basically sort the graph using the DS. Is it right? Is this given the needed perspective for the graph? Yeah. Maybe I can start from topological sort itself. Maybe I should dive into how to make it.
Red Maelstrom: Okay.
Electric Possum: Um. Topological sort, topological outcome, topological sort. So number of nodes. And one more thing here is so since dfs using stack, I need to make the stack and I need to let's make like dfs first for the one node because I will go through the minimal to the bigger. So for adjusted list is. But is it like an index? Let's imagine for a second that is just ticked this, but I'm just trying to remember the DFS itself. Enable just at least node. I need to execute DFS on it, not on the node, but on the neighbor. And since I'm using stack, I'm using here stack node and I need a condition here the condition would be visited, it should be like this. So oh I see, I'm using global context here. But since we are in the one function, this would be enough. So here I should check if it visited and dfs it only if it's not. So this will eliminate the necessary visits equal contain result and visited would be each node is not visited at the first stage. Yeah. Let me quickly check what will be going here. So for each node this node would be visited. This is node index probably. And for neighbor in edge list node, if neighbor is not visited, then dfs neighbor but visited here should be shared and node will go to the stack since it would be sorted. So I should make it somehow like this. Yeah. I'm isn't visited from the outer scope here. It's not very correct way to do these things. Do I have an option to run this code or not?
Red Maelstrom: You can run this code.
Electric Possum: Yeah, it won't work.
Red Maelstrom: At top left there is run button.
Electric Possum: Sorry.
Red Maelstrom: On top left there is a button.
Electric Possum: For I seem a second.
Red Maelstrom: Okay, I'm just taking a look at it going to the DF. Where are you building the graph? Okay, you're using the same answer as a list.
Electric Possum: Yeah. I now think that we can iterate through the result of this sort and check the node in the original.
Red Maelstrom: If you see these output is nothing but the topological number for those nodes, right. In topological order, what is the number? Right? Yes, this output is that. Okay. And the next you will use this order to run the DFS. Okay.
Electric Possum: I mean I wanted to use this order like 345012 to iterate through the original list and for each list, for each item to check his neighbors and if his neighbors persist here then increments the available distances for the given node. Is it working like. Oh no, it's not. I might actually be wrong around what is exactly topological sort is. I'm feeling that I'm missing something here, but at the same time it looks like. I have an idea of how exact this problem could look like. Yeah, I'm just trying to. So now going through this loop is to create something like you. And now this is the note three. What about here? The note the node three. The node three. The node three actually has two neighbors. Here's looks like something wrong with complexity here. So we are checking neighbors in a regional graph here. Checking the node here. And we see like two and a half. We need to increment the values. Something wrong here. Something wrong here. But I can keep it in my head. So I'll try to use prints. So it would be longest, longest party, like one. It. It's looking like I forgot missing some conditions here. I think.
Red Maelstrom: The entries are same, but somehow the output sequence is different. Right. So the actual output also has two ones, two twos and two zeros. And yours also has same, but the sequence is jumbled. Right. Because you are going through the topological sort, but the actual output is expected as the actual number. That's the difference. I guess.
Electric Possum: It, yes, sorry.
Red Maelstrom: So basically you got an ordered graph, right? You got an ordered graph, but from ordered graph. Actually you can use the order graph again back to build the result, right?
Electric Possum: Yes.
Red Maelstrom: You already have this distance. So you can just look at index of zero. In order graph is, let's say something else. So the distance of zero goes to that place and build your result accordingly. Did you get what I'm trying to say?
Electric Possum: Yeah, I'm thinking I missed some words here. I got slightly inversed result, but I'm not sure whether it completely correct or not. It's just a random idea to make this, but. Yeah, it's obviously not correct. So what is the problem here? I'm going through the node, right? For the node three. Where is the node three? For the node three, I'm checking the neighbors. The neighbors are two and four. So the distance for that node. So this is the distance for this node. This should be the distance from the top of their graph to this node. This is the. Yeah, but I can't simply inverse it and said that for the node three, what should be improved is. Yeah, I'm actually. Should not increase the distance for the neighbor. No, I should increase the distance of the neighbor, but I should increase distance of the neighbor. I guess conditionally like if the distance of the neighbor is less than distance of the node plus one, or it's absolute idea. I'm not sure at this point. I'll try. No, it's not fixing anything.
Red Maelstrom: No. So basically what I'm trying to say is your ordered graph, right, has the order in which your nodes would be processed, right?
Electric Possum: Just as I ordered the graph, yeah.
Red Maelstrom: So basically, if the zero node in the ordered list is, let's say, two, right, then the distance calculated in distance graph at zero place is a distance for the node two. So you can use the ordered graph to create the result from the distance. That's what I'm saying. Do you follow?
Electric Possum: Yeah. Yes.
Red Maelstrom: What I'm trying to say is, let's say that you have a new result array. Result of ordered of I would equals to distance of I for every node. For every I from zero to the length of. You can do this and then I can print the don't. Do you follow? Till now? Like the, the ordered graph of zero of I is nothing but the ith node. Like if I say that ordered graph of I is equals to j, right. So it means that the distance of I is nothing but the distance of. Actually the distance is nothing but actually the distance of death node, right?
Electric Possum: Right.
Red Maelstrom: So what you need to do is. Yeah, for ordered for I in range length of ordered graph, you do. Result of I is nothing. Result of ordered of, sorry.
Electric Possum: Result of order.
Red Maelstrom: Nothing but instance of I. Yeah, I'm just printing the result. We just need to declare the result. How to declare for length? I'm not sure in python like how to declare. Okay, you already did above it. Can you run?
Electric Possum: It's probably we need.
Red Maelstrom: I don't know why it's still printing this. I think this should have made it. We saw restructured print ordered graph once.
Electric Possum: Maybe we can just iterate through the ordered graph without the index. Maybe the problem is here. Okay, I mean, tab I type here too. 50 one.
Red Maelstrom: Okay. Y Index Y 30012. I think two should be the first node in the ordered list. Right, because two has a zero out degree. I think there is some flaw in the way ordered graph is calculated. Instead of three, the two should be the zero.
Electric Possum: Maybe the issue was here. No, it wasn't. You mean that our first node should be two, not the free, right, right.
Red Maelstrom: But yeah, I think I understood the idea. The implementation also looks very close to correct. Very small issue, but yeah, that's okay. I have one follow up on this. Does this question work for cyclic graph? If graph contains cycle, will this code work.
Electric Possum: I think if graph contains cycle, it's unsolvable.
Red Maelstrom: Okay. It's solvable, but there would be some modification required. Basically, it depends how we represent the cyclic for cycle. We will represent the distance of node which are part of cycle as infinity.
Electric Possum: Yeah. Okay. What I meant is that if the graph contains cycles, there is a possibility that the longest path is indefinite. Right.
Red Maelstrom: We can just write it as infinite.
Electric Possum: Yeah. We need to have some constraint or use it. Sorry, you wanted to ask something.
Red Maelstrom: So what you are trying to do is with some constraint, it is possible to modify your code. Is that correct?
Electric Possum: M. But this would be like. I'm not sure this would be solvable in how it called polynomial time. Since we have a cycle and we need to define a way to find the cycle and deal with the situation where the path is infinite. Yeah. Most likely to find the cycles, we need to add some, like.
Red Maelstrom: Composited.
Electric Possum: Some function that would find loops in the graph. A cycles in the graph.
Red Maelstrom: Okay.
Electric Possum: As the stage of the solution. Probably even before they're sorting the graph or in the process of sorting.
Red Maelstrom: Cool. Awesome. I think those were the questions from my side.
Electric Possum: Okay.
Red Maelstrom: So, yeah. I will be writing a detailed feedback after this.
Electric Possum: I see. Cool. Yeah. Thank you for the interview. I needed to think through this interview and it was a pleasure. Thank you.
Red Maelstrom: Yeah. Have a nice weekend.
Electric Possum: You too. Bye.