Skip to content

Completed Design-1#2638

Open
sarvanibaru wants to merge 1 commit intosuper30admin:masterfrom
sarvanibaru:master
Open

Completed Design-1#2638
sarvanibaru wants to merge 1 commit intosuper30admin:masterfrom
sarvanibaru:master

Conversation

@sarvanibaru
Copy link

No description provided.

@super30admin
Copy link
Owner

Strengths:

  • The code for both problems is well-structured and readable, with comments explaining the approach.
  • The MinStack solution is correctly implemented and efficient.

Areas for Improvement:

  1. For the HashSet problem, the primary hash function should use modulo operation to ensure the index is within the range [0, primaryBuckets-1]. The current method using division may not distribute keys properly and could lead to index out of bounds for large keys.
  2. The secondary hash function should use modulo for the primary hash and division for the secondary hash, as in the reference solution. This ensures that the secondary index is computed correctly.
  3. The student's solution does not handle the edge case for key=1000000. When primary index is 0, the secondary array should be of size 1001 to accommodate secondary index 1000. Otherwise, for key=1000000, the secondary index would be 1000, which is beyond the array size of 1000.
  4. Consider using meaningful variable names. For example, arr could be renamed to storage or buckets for clarity.

Suggested Correction for HashSet:

  • Change getPrimaryHash to return key % primaryBuckets;
  • Change getSecondaryHash to return key / secondaryBuckets; (with secondaryBuckets=1000)
  • In the add method, when creating the secondary array for primary index 0, create an array of size 1001 instead of 1000.

Example corrected code snippet for MyHashSet:

class MyHashSet {
    boolean[][] storage;
    int primaryBuckets;
    int secondaryBuckets;

    public MyHashSet() {
        this.primaryBuckets = 1000;
        this.secondaryBuckets = 1000;
        this.storage = new boolean[primaryBuckets][];
    }

    private int getPrimaryHash(int key) {
        return key % primaryBuckets;
    }

    private int getSecondaryHash(int key) {
        return key / secondaryBuckets;
    }

    public void add(int key) {
        int primaryIndex = getPrimaryHash(key);
        if (storage[primaryIndex] == null) {
            if (primaryIndex == 0) {
                storage[primaryIndex] = new boolean[secondaryBuckets + 1];
            } else {
                storage[primaryIndex] = new boolean[secondaryBuckets];
            }
        }
        int secondaryIndex = getSecondaryHash(key);
        storage[primaryIndex][secondaryIndex] = true;
    }

    // remove and contains methods similarly adjusted
}

@sarvanibaru
Copy link
Author

Strengths:

  • The code for both problems is well-structured and readable, with comments explaining the approach.
  • The MinStack solution is correctly implemented and efficient.

Areas for Improvement:

  1. For the HashSet problem, the primary hash function should use modulo operation to ensure the index is within the range [0, primaryBuckets-1]. The current method using division may not distribute keys properly and could lead to index out of bounds for large keys.
  2. The secondary hash function should use modulo for the primary hash and division for the secondary hash, as in the reference solution. This ensures that the secondary index is computed correctly.
  3. The student's solution does not handle the edge case for key=1000000. When primary index is 0, the secondary array should be of size 1001 to accommodate secondary index 1000. Otherwise, for key=1000000, the secondary index would be 1000, which is beyond the array size of 1000.
  4. Consider using meaningful variable names. For example, arr could be renamed to storage or buckets for clarity.

Suggested Correction for HashSet:

  • Change getPrimaryHash to return key % primaryBuckets;
  • Change getSecondaryHash to return key / secondaryBuckets; (with secondaryBuckets=1000)
  • In the add method, when creating the secondary array for primary index 0, create an array of size 1001 instead of 1000.

Example corrected code snippet for MyHashSet:

class MyHashSet {
    boolean[][] storage;
    int primaryBuckets;
    int secondaryBuckets;

    public MyHashSet() {
        this.primaryBuckets = 1000;
        this.secondaryBuckets = 1000;
        this.storage = new boolean[primaryBuckets][];
    }

    private int getPrimaryHash(int key) {
        return key % primaryBuckets;
    }

    private int getSecondaryHash(int key) {
        return key / secondaryBuckets;
    }

    public void add(int key) {
        int primaryIndex = getPrimaryHash(key);
        if (storage[primaryIndex] == null) {
            if (primaryIndex == 0) {
                storage[primaryIndex] = new boolean[secondaryBuckets + 1];
            } else {
                storage[primaryIndex] = new boolean[secondaryBuckets];
            }
        }
        int secondaryIndex = getSecondaryHash(key);
        storage[primaryIndex][secondaryIndex] = true;
    }

    // remove and contains methods similarly adjusted
}

Hi,

I see this as a secondary/alternative approach if we do not want to explicitly check 0th index condition and overflow. We initialized the primary bucket with size as 1001 to handle that division scenario. This approach was also suggested by the instrustor in the class and got accepted in leetcode. Could you please explain if I am missing anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants