educative.io

Issue: intersectionWithHashing

Good morning,
for whatever reason the intersectionWithHashing solution is not accepted.

Checking the code with my debugger seems like my solution is fine.


public static <T> SinglyLinkedList<T> intersectionWithHashing(SinglyLinkedList<T> list1, SinglyLinkedList<T> list2) {
            Set<T> visited1 = new LinkedHashSet<>();
            Set<T> visited2 = new LinkedHashSet<>();
            SinglyLinkedList<T>.Node p1 = list1.getHeadNode();
            SinglyLinkedList<T>.Node p2 = list2.getHeadNode();
            SinglyLinkedList<T> result = new SinglyLinkedList<T>();
            result.insertAtHead(null);
            while(p1 != null) {
                visited1.add(p1.data);
                p1 = p1.nextNode;
            }

            while(p2 != null) {
                visited2.add(p2.data);
                p2 = p2.nextNode;
            }

            for (T el: visited1) {
                if(visited2.contains(el)) {
                    result.insertAtEnd(el);
                }
            }

            result.deleteAtHead();
        return result;
    }

Course: https://www.educative.io/collection/5642554087309312/5724822843686912
Lesson: https://www.educative.io/collection/page/5642554087309312/5724822843686912/5031862374236160

Yea i think the expected output for the intersection test cases are wrong.

Hi @Davide_Pugliese,
Thanks for reaching out! I understand you’re having some trouble with the import packages. Don’t worry, your code actually looks great and works in the lesson. Just a quick tip: if you add
import java.util.Set;
import java.util.LinkedHashSet;
at the start of your code, you’ll get the expected output, and it’ll work perfectly in the playground too.

We trust that Educative has been a source of inspiration for your ongoing learning journey. If you have any further questions or concerns, feel free to reach out. Thank you for your engagement!

Happy learning!

Best regards,
Kanwal from Educative

1 Like