Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(MiniMaxAlgorithm): correct power-of-2 validation and log2 handling + add unit tests #6581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Harsh-dubey-git wants to merge 1 commit into TheAlgorithms:master
base: master
Choose a base branch
Loading
from Harsh-dubey-git:fix-minimax-bug

Conversation

Copy link

@Harsh-dubey-git Harsh-dubey-git commented Oct 1, 2025

Description

This PR fixes a bug in MiniMaxAlgorithm.java related to incorrect validation of power-of-2 array lengths in the setScores() method and improves edge case handling in the log2() method. Additionally, it adds a comprehensive JUnit 5 test suite to verify correctness and prevent future regressions.

Changes Made

  1. setScores()

    • Added validation for null and empty arrays.
    • Correctly checks that the array length is a power of 2 using (length & (length - 1)) == 0.
    • Throws IllegalArgumentException for invalid inputs.
  2. log2()

    • Handles invalid inputs (0 or negative numbers) safely.
    • Recursively calculates log2 for valid power-of-2 integers.
    • Optionally throws exceptions for non-power-of-2 numbers (ensures robustness).
  3. Unit Tests

    • MiniMaxAlgorithmTest.java includes tests for:
      • Valid and invalid setScores() inputs.
      • Edge cases and invalid inputs for log2().
      • Correct miniMax() computation for deterministic score arrays.
    • Uses reflection to test private log2() method.
    • Ensures proper array lengths, heights, and exception handling.
  4. Other Improvements

    • Tests verify the algorithm behaves correctly for maximizer and minimizer scenarios.
    • Helps prevent regressions for future contributors.

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

@DenizAltunkapan DenizAltunkapan Awaiting requested review from DenizAltunkapan DenizAltunkapan is a code owner

@yanglbme yanglbme Awaiting requested review from yanglbme yanglbme is a code owner

@alxkm alxkm Awaiting requested review from alxkm alxkm is a code owner

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant

AltStyle によって変換されたページ (->オリジナル) /