Блог пользователя Dsxv

Автор Dsxv, история, 5 лет назад, По-английски

Hi Everyone!

I haven't been active lately because of interviews going on. Recently I appeared for Amazon Interview for Internship and there was a problem that I still have doubts with:

Problem

I confirmed with the interviewer that there is a formation of the cycle in this, he told me that cycle formation is possible and whenever it happens you have to break the chain there since you don't want to repeat the same elements

As you can see the graph is not an unweighted DAG, Hence, the problem became finding acyclic longest chain in a directed cyclic graph.

I wasn't able to come with a clear solution (O(n)) because it didn't feel right that how I can take care of cases with a cycle using maybe topological sort? So I wanted to ask what is the best possible complexity answer for this.

Thanks!

UPD: The problem might not even be of a graph, I have mentioned the original problem in the exact words as by the interviewer, not that you have to follow the graph approach, I just want to find any possible views on this.

UPD2: Got selected anyway lol :p

  • Проголосовать: нравится
  • +17
  • Проголосовать: не нравится

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +2 Проголосовать: не нравится

There are only 26 vertices, so it may be possible to do a DFS for each vertex, using each edge at most once.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I think we can do dp of some sort like in this problem

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

If this was an interview question, then in the interviews, they often don't tell the complete problem to the candidate, and allow the candidate to ask questions and interact like —

What are the constraints of problem or questions like these.

Now, this might seem senseless, but, did you ask, that "what should I do (What value shall I return), in case the answer becomes infinite" ?

I guess, you yourself converted it into — "Find longest acyclic chain in the graph". Maybe, the formal problem was never, what you think.

I would have responded with — "For some graphs, the answer will be infinite, so Mr Interviewer, could you please tell me, what shall I return in that case?"

And, if it is not infinite, then answer will be max path length in an un-directed graph. And, to check, whether answer will be infinite or not, I can "detect if a cycle exists in the given graph"

If you did ask these questions, and the problem still translates to what you described above, then yes, its tricky ( But I still believe, that this might not be the case ).

  • »
    »
    5 лет назад, скрыть # ^ |
    Rev. 2  
    Проголосовать: нравится 0 Проголосовать: не нравится

    Sorry for giving less info because I wanted to focus on the question. Yes I did, let me update my problem statement with that too.

    UPD: I most certainly realized it will get messy if I am going to solve this specific problem, that's why I spent almost 5 minutes confirming if this is what he wants. And he said that it can be done in O(n).

»
5 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Somehow, the comment got posted twice, ignore

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Dsxv (previous revision, new revision, compare).

»
5 лет назад, скрыть # |
 
Проголосовать: нравится +4 Проголосовать: не нравится

You can solve this problem using SCC and dynamic programming . See in the given graph there may be some scc which means you can reach to every other node in that scc . so count all the nodes in the scc and compress it into a single node .
After that apply dynamic programming .(which is not hard now) Here is the link to a similar problem.
I hope I understood your problem correctly .

  • »
    »
    5 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится

    I might be wrong, but I think you can repeat your path in this problem, just that coins will be added only once. This means SCC's value can be taken as a single value, however, in my problem you have to choose which path in SCC you have to take or if you should loop inside.

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

This problem is very much similar to the problem that came in the google kickstart round-H 2020... you may refer to that problem for a better solution.. link to the problem

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Dsxv (previous revision, new revision, compare).