Skip to content

Competitive_Coding-3#1181

Open
Hriday-A wants to merge 1 commit intosuper30admin:masterfrom
Hriday-A:master
Open

Competitive_Coding-3#1181
Hriday-A wants to merge 1 commit intosuper30admin:masterfrom
Hriday-A:master

Conversation

@Hriday-A
Copy link
Copy Markdown

@Hriday-A Hriday-A commented May 2, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Interview Problem : Pascal's Triangle (pascals_triangle.java)

Strengths:

  • You correctly identified that each row in Pascal's Triangle is built from the previous row.
  • You understood that the first and last elements of each row are always 1.

Areas for Improvement:

  1. Logic Error: The key issue is in how you're accessing elements from the previous row. For generating row i (0-indexed), you should:

    • Start with a row of length i+1.
    • Set the first and last elements to 1.
    • For the middle elements (from index 1 to i-1), each element should be the sum of the element at the same index and the previous index from the previous row.

    Your current code tries to use nums.get(j-1) and nums.get(j) from the previous row, but when j is 0, you're accessing index -1 which is invalid. Also, when j equals i, you're accessing index i in the previous row which only has i elements (indices 0 to i-1), causing an index error.

  2. Row Indexing: Remember that row i has i+1 elements. The previous row (which is row i-1) has i elements. So when building row i, you should iterate j from 0 to i, but for the middle elements, you should sum prevRow.get(j-1) and prevRow.get(j) only when both indices are valid.

  3. Code Clarity: Use more descriptive variable names. For example:

    • Instead of nums2, use firstRow.
    • Instead of nums1, use newRow.
    • Instead of nums, use prevRow.
  4. Initialization: Your initial row is correct, but you need to handle the case when numRows is 0. However, the constraints say numRows >= 1, so it's acceptable.

Suggested Correct Approach:
Here's a corrected version of your code:

class Solution {
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> triangle = new ArrayList<>();
        if (numRows == 0) return triangle;
        
        // First row
        List<Integer> firstRow = new ArrayList<>();
        firstRow.add(1);
        triangle.add(firstRow);
        
        for (int i = 1; i < numRows; i++) {
            List<Integer> prevRow = triangle.get(i - 1);
            List<Integer> newRow = new ArrayList<>();
            
            // First element is always 1
            newRow.add(1);
            
            // Middle elements: sum of two elements from the previous row
            for (int j = 1; j < i; j++) {
                newRow.add(prevRow.get(j - 1) + prevRow.get(j));
            }
            
            // Last element is always 1
            newRow.add(1);
            
            triangle.add(newRow);
        }
        
        return triangle;
    }
}

This code correctly generates each row by:

  • Adding 1 at the start
  • Adding the sums of adjacent elements from the previous row for the middle
  • Adding 1 at the end

VERDICT: NEEDS_IMPROVEMENT


Interview Problem: Pairs with K difference (k_diff_pairs.java)

Your solution is on the right track, but there is a critical issue when k=0. The problem requires counting unique pairs. For k=0, we need to count pairs of indices (i, j) with i != j and nums[i] = nums[j]. Your current approach uses a HashMap that maps each number to its last index. This means that if a number appears multiple times, only the last index is stored. So when you check for k=0, you are looking for the same number, but if the current index i is the same as the stored index (which is the last occurrence), then you skip it. However, if there are multiple occurrences, you should be able to form a pair as long as there is at least one other index.

To fix this, you should store the frequency of each number instead of the last index. Then, for k>0, you can check for the complement as you do now, but for k=0, you can check if the frequency of the number is greater than 1. Alternatively, you can use a frequency map and then iterate over the keys.

Here's a better approach:

  • Use a frequency map (number -> count).
  • If k == 0, then count how many numbers have frequency >= 2.
  • If k > 0, then for each number num, check if num + k exists in the map. Since we want unique pairs, we can iterate over the keys and avoid duplicates.

Also, you can avoid creating sorted arrays for each pair by always considering the pair (min, max) and storing it in a set. However, since k is non-negative, for k>0, the pairs (a, b) and (b, a) are the same if we sort. But in your current code, you are sorting each pair, which is acceptable but creates unnecessary objects.

Another improvement: instead of storing pairs in a set, you can use a set of integers for the numbers that have been used to form a pair. For example, when k>0, for each number num, if we find num + k in the map, we can add num to a set of "used" numbers? Actually, we want to avoid counting duplicate pairs. Alternatively, we can iterate over the unique numbers.

Recommended solution:

  1. Create a frequency map.
  2. If k == 0:
    • Count the number of keys with frequency >= 2.
  3. Else:
    • Create a set of numbers.
    • For each number num in the set, if set contains num + k, then increment count.
    • Return count.

This avoids the issue with indices and is more efficient.

Your code also has a minor issue: you are checking both num-k and num+k, which is redundant because (num, num-k) is the same as (num-k, num) when sorted. So you only need to check one direction. For example, check only num+k to avoid duplicates.

So overall, your solution needs to be revised for k=0 and to avoid redundant checks.

VERDICT: NEEDS_IMPROVEMENT

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