Pregunta de entrevista de Yahoo

How to detect a common node given two linked lists??

Respuestas de entrevistas

Anónimo

17 de dic de 2009

firstly traverse both the linked lists, let their length be l1 and l2 with l1

1

Anónimo

4 de feb de 2021

The key in these questions is to cover the fundamentals, and be ready for the back-and-forth with the interviewer. Might be worth doing a mock interview with one of the Yahoo or ex-Yahoo Engineer experts on Prepfully? They give real-world practice and guidance, which is pretty helpful. prepfully.com/practice-interviews

Anónimo

15 de dic de 2009

Naive approach would be to use a simple nested loop to iterate over the two lists one inside another. This would take O(n^2) runtime, which is not too good. A better approach would be to use a separate hash map data structure and iterate the two lists separately like the following: final Map map = new HashMap(); final int s1 = a1.length; for(int i = 0; i < s1; i++) map.put(a1[i], a1[i]); final int s2 = a2.length; for(int i = 0; i < s2; i++) { final int v = a2[i]; if(map.containsKey(v)) return v; } throw new RuntimeException("No common value found."); The above is just to show the algorithm, replace integer with node objects. This will allow runtime of O(n) time assuming the values hash key is well spread. but of course, you will end up with O(n) memory consumption.