• + 4 comments

    In Python you can save a substantial amount of memory when creating a large number of objects by defining __slots__.

    In the Python code: https://www.hackerrank.com/challenges/contacts/submissions/code/28219710 changing the Node class from:

    class Node:
        def __init__(self):
            self.ch={}
            self.count=0
    

    To:

    class Node(dict):
        __slots__ = ['count']
        def __init__(self):
            self.count=0
    

    Where Node is subclassed from dict and __slots__ is set manually to have a counter, saves enough memory to allow the code to run without a segmentation fault.