Adding boilerplate code and test cases looking up leetcode with AI

This commit is contained in:
hitanshu310 2025-02-28 15:50:42 +05:30
parent dd218447e3
commit b57b6f14eb
43 changed files with 890 additions and 1 deletions

9
.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf
# These are Windows script files and should use crlf
*.bat text eol=crlf

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle
# Ignore Gradle build output directory
build

View File

@ -4,4 +4,4 @@
1. [Leetcode Top 150](https://leetcode.com/studyplan/top-interview-150/)
2. [Striver's SDE sheet](https://takeuforward.org/interviews/strivers-sde-sheet-top-coding-interview-problems)
3. [Team Blind Top 75 Blind](https://www.teamblind.com/post/new-year-gift---curated-list-of-top-75-leetcode-questions-to-save-your-time-oam1oreu)
3. [Team Blind Top 75](https://www.teamblind.com/post/new-year-gift---curated-list-of-top-75-leetcode-questions-to-save-your-time-oam1oreu)

42
dsa/.gitignore vendored Normal file
View File

@ -0,0 +1,42 @@
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

3
dsa/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

17
dsa/.idea/gradle.xml generated Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
</GradleProjectSettings>
</option>
</component>
</project>

7
dsa/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17 (2)" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
dsa/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

19
dsa/build.gradle Normal file
View File

@ -0,0 +1,19 @@
plugins {
id 'java'
}
group = 'com.hithomelabs.dsa'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation platform('org.junit:junit-bom:5.10.0')
testImplementation 'org.junit.jupiter:junit-jupiter'
}
test {
useJUnitPlatform()
}

BIN
dsa/gradle/wrapper/gradle-wrapper.jar vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,6 @@
#Wed Feb 26 15:43:32 IST 2025
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

234
dsa/gradlew vendored Executable file
View File

@ -0,0 +1,234 @@
#!/bin/sh
#
# Copyright © 2015-2021 the original authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
##############################################################################
#
# Gradle start up script for POSIX generated by Gradle.
#
# Important for running:
#
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
# noncompliant, but you have some other compliant shell such as ksh or
# bash, then to run this script, type that shell name before the whole
# command line, like:
#
# ksh Gradle
#
# Busybox and similar reduced shells will NOT work, because this script
# requires all of these POSIX shell features:
# * functions;
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
# * compound commands having a testable exit status, especially «case»;
# * various built-in commands including «command», «set», and «ulimit».
#
# Important for patching:
#
# (2) This script targets any POSIX shell, so it avoids extensions provided
# by Bash, Ksh, etc; in particular arrays are avoided.
#
# The "traditional" practice of packing multiple parameters into a
# space-separated string is a well documented source of bugs and security
# problems, so this is (mostly) avoided, by progressively accumulating
# options in "$@", and eventually passing that to Java.
#
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
# see the in-line comments for details.
#
# There are tweaks for specific operating systems such as AIX, CygWin,
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
#
##############################################################################
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
app_path=$0
# Need this for daisy-chained symlinks.
while
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
[ -h "$app_path" ]
do
ls=$( ls -ld "$app_path" )
link=${ls#*' -> '}
case $link in #(
/*) app_path=$link ;; #(
*) app_path=$APP_HOME$link ;;
esac
done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/}
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
warn () {
echo "$*"
} >&2
die () {
echo
echo "$*"
echo
exit 1
} >&2
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
nonstop=false
case "$( uname )" in #(
CYGWIN* ) cygwin=true ;; #(
Darwin* ) darwin=true ;; #(
MSYS* | MINGW* ) msys=true ;; #(
NONSTOP* ) nonstop=true ;;
esac
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD=$JAVA_HOME/jre/sh/java
else
JAVACMD=$JAVA_HOME/bin/java
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
fi
# Collect all arguments for the java command, stacking in reverse order:
# * args from the command line
# * the main class name
# * -classpath
# * -D...appname settings
# * --module-path (only if needed)
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
# For Cygwin or MSYS, switch paths to Windows format before running java
if "$cygwin" || "$msys" ; then
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
JAVACMD=$( cygpath --unix "$JAVACMD" )
# Now convert the arguments - kludge to limit ourselves to /bin/sh
for arg do
if
case $arg in #(
-*) false ;; # don't mess with options #(
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
[ -e "$t" ] ;; #(
*) false ;;
esac
then
arg=$( cygpath --path --ignore --mixed "$arg" )
fi
# Roll the args list around exactly as many times as the number of
# args, so each arg winds up back in the position where it started, but
# possibly modified.
#
# NB: a `for` loop captures its iteration list before it begins, so
# changing the positional parameters here affects neither the number of
# iterations, nor the values presented in `arg`.
shift # remove old arg
set -- "$@" "$arg" # push replacement arg
done
fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
-classpath "$CLASSPATH" \
org.gradle.wrapper.GradleWrapperMain \
"$@"
# Use "xargs" to parse quoted args.
#
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
#
# In Bash we could simply go:
#
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
# set -- "${ARGS[@]}" "$@"
#
# but POSIX shell has neither arrays nor command substitution, so instead we
# post-process each arg (as a line of input to sed) to backslash-escape any
# character that might be a shell metacharacter, then use eval to reverse
# that process (while maintaining the separation between arguments), and wrap
# the whole thing up as a single "set" statement.
#
# This will of course break if any of these variables contains a newline or
# an unmatched quote.
#
eval "set -- $(
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
xargs -n1 |
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
tr '\n' ' '
)" '"$@"'
exec "$JAVACMD" "$@"

89
dsa/gradlew.bat vendored Normal file
View File

@ -0,0 +1,89 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto execute
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto execute
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega

View File

@ -0,0 +1,9 @@
I am trying to check in my leetcode solutions to a gradle project . Help me reduce my manual work.
For each question present except the first one,
1. Create a new package for the problem inside /dsa/src/main/java with package name com.hithomelabs.dsa.array.<package_name> where package name is the name of the question in all lower case with '_' instead of spaces.
2. Create a new java file with a public class Solution as in the leetcode URL associated with each question.
3. Create a new java file for a package protected interface Solvable belonging to the above package with a method solve having the same return type and arguments as of the metod present in the class Solution.

2
dsa/settings.gradle Normal file
View File

@ -0,0 +1,2 @@
rootProject.name = 'dsa'

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock;
public class Solution implements Solvable {
@Override
public int solve(int[] prices) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,6 @@
package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock;
public interface Solvable {
public int solve(int[] prices);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.container_with_most_water;
public class Solution implements Solvable {
@Override
public int solve(int[] height) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.container_with_most_water;
public interface Solvable {
public int solve(int[] height);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.product_of_array_except_self;
public class Solution implements Solvable {
@Override
public int[] solve(int[] nums) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.contains_duplicate;
public interface Solvable {
public boolean solve(int[] nums);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array;
public class Solution implements Solvable {
@Override
public int solve(int[] nums) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array;
public interface Solvable {
public int solve(int[] nums);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.maximum_product_subarray;
public class Solution implements Solvable {
@Override
public int solve(int[] nums) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.maximum_product_subarray;
public interface Solvable {
public int solve(int[] nums);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.product_of_array_except_self;
public class Solution {
// Method signature for the solution
public int[] solve(int[] nums) {
// Implementation here
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.product_of_array_except_self;
public interface Solvable {
public int[] solve(int[] nums);
}

View File

@ -0,0 +1,8 @@
package com.hithomelabs.dsa.array.search_in_rotated_sorted_array;
public class Solution implements Solvable {
@Override
public int solve(int[] nums, int target) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.search_in_rotated_sorted_array;
public interface Solvable {
public int solve(int[] nums, int target);
}

View File

@ -0,0 +1,10 @@
package com.hithomelabs.dsa.array.three_sum;
import java.util.List;
public class Solution implements Solvable {
@Override
public List<List<Integer>> solve(int[] nums) {
// Implementation of the solution
}
}

View File

@ -0,0 +1,7 @@
package com.hithomelabs.dsa.array.three_sum;
import java.util.List;
public interface Solvable {
public List<List<Integer>> solve(int[] nums);
}

View File

@ -0,0 +1,26 @@
package com.hithomelabs.dsa.array.two_sum;
import java.util.Map;
public class HashMapSolution implements Solvable {
@Override
public int[] solve(int[] nums, int target) {
// Create a map to store the numbers and their corresponding indices
Map<Integer, Integer> map = new HashMap();
// Iterate through the array of numbers
for (int i = 0; i < nums.length; i++){
// Check if the complement (target - current number) exists in the map
if (map.containsKey(target - nums[i])){
// If found, return the indices of the current number and its complement
int[] solution = {i, map.get(target - nums[i])};
return solution;
}
else
// If not found, add the current number and its index to the map
map.put(nums[i], i);
}
// Return null if no solution is found
return null;
}
}

View File

@ -0,0 +1,10 @@
package com.hithomelabs.dsa.array.two_sum;
class Solution {
private Solution solution = new HashMapSolution();
public int[] twoSum(int[] nums, int target) {
return solution.solve(nums, target);
}
}

View File

@ -0,0 +1,5 @@
package com.hithomelabs.dsa.array.two_sum;
public interface Solvable {
public int[] solve(int[] nums, int target);
}

View File

@ -0,0 +1,35 @@
package com.hithomelabs.dsa.array.best_time_to_buy_and_sell_stock;
import static org.junit.jupiter.api.Assertions.assertEquals;
// https://example.com/best_time_to_buy_and_sell_stock
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case with increasing prices
int[] prices1 = {7, 1, 5, 3, 6, 4};
assertEquals(5, solution.solve(prices1));
// Test case 2: Prices decrease
int[] prices2 = {7, 6, 4, 3, 1};
assertEquals(0, solution.solve(prices2));
// Test case 3: Prices are constant
int[] prices3 = {5, 5, 5, 5, 5};
assertEquals(0, solution.solve(prices3));
// Test case 4: Prices with a single day
int[] prices4 = {1};
assertEquals(0, solution.solve(prices4));
// Test case 5: Prices with two days, increasing
int[] prices5 = {1, 2};
assertEquals(1, solution.solve(prices5));
// Test case 6: Prices with two days, decreasing
int[] prices6 = {2, 1};
assertEquals(0, solution.solve(prices6));
}
}

View File

@ -0,0 +1,31 @@
package com.hithomelabs.dsa.array.container_with_most_water;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Example case
int[] heights1 = {1,8,6,2,5,4,8,3,7};
assertEquals(49, solution.solve(heights1));
// Test case 2: Minimum input
int[] heights2 = {1, 1};
assertEquals(1, solution.solve(heights2));
// Test case 3: All heights are the same
int[] heights3 = {4, 4, 4, 4};
assertEquals(12, solution.solve(heights3));
// Test case 4: Decreasing heights
int[] heights4 = {5, 4, 3, 2, 1};
assertEquals(6, solution.solve(heights4));
// Test case 5: Increasing heights
int[] heights5 = {1, 2, 3, 4, 5};
assertEquals(6, solution.solve(heights5));
}
}

View File

@ -0,0 +1,29 @@
package com.hithomelabs.dsa.array.contains_duplicate;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Example case with duplicates
int[] nums1 = {1, 2, 3, 1};
assertTrue(solution.containsDuplicate(nums1));
// Test case 2: No duplicates
int[] nums2 = {1, 2, 3, 4};
assertFalse(solution.containsDuplicate(nums2));
// Test case 3: All elements are the same
int[] nums3 = {1, 1, 1, 1};
assertTrue(solution.containsDuplicate(nums3));
// Test case 4: Empty array
int[] nums4 = {};
assertFalse(solution.containsDuplicate(nums4));
// Test case 5: Single element
int[] nums5 = {1};
assertFalse(solution.containsDuplicate(nums5));
}
}

View File

@ -0,0 +1,29 @@
package com.hithomelabs.dsa.array.find_minimum_in_rotated_sorted_array;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case with rotation
int[] nums1 = {3, 4, 5, 1, 2};
assertEquals(1, solution.solve(nums1));
// Test case 2: Array not rotated
int[] nums2 = {1, 2, 3, 4, 5};
assertEquals(1, solution.solve(nums2));
// Test case 3: Array with one element
int[] nums3 = {1};
assertEquals(1, solution.solve(nums3));
// Test case 4: Array with two elements, rotated
int[] nums4 = {2, 1};
assertEquals(1, solution.solve(nums4));
// Test case 5: Array with duplicates
int[] nums5 = {2, 2, 2, 0, 1, 2};
assertEquals(0, solution.solve(nums5));
}
}

View File

@ -0,0 +1,33 @@
package com.hithomelabs.dsa.array.maximum_product_subarray;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case with positive numbers
int[] nums1 = {2, 3, -2, 4};
assertEquals(6, solution.solve(nums1));
// Test case 2: Array with all negative numbers
int[] nums2 = {-2, -3, -1};
assertEquals(6, solution.solve(nums2));
// Test case 3: Array with a mix of positive and negative numbers
int[] nums3 = {-2, 0, -1};
assertEquals(0, solution.solve(nums3));
// Test case 4: Array with one element
int[] nums4 = {3};
assertEquals(3, solution.solve(nums4));
// Test case 5: Array with two elements, one negative
int[] nums5 = {2, -5};
assertEquals(2, solution.solve(nums5));
// Test case 6: Array with multiple zeros
int[] nums6 = {0, 2, 0, 3, 0};
assertEquals(6, solution.solve(nums6));
}
}

View File

@ -0,0 +1,34 @@
package com.hithomelabs.dsa.array.product_of_array_except_self;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case
int[] nums1 = {1, 2, 3, 4};
int[] expected1 = {24, 12, 8, 6};
assertArrayEquals(expected1, solution.solve(nums1));
// Test case 2: Array with one element
int[] nums2 = {5};
int[] expected2 = {1};
assertArrayEquals(expected2, solution.solve(nums2));
// Test case 3: Array with two elements
int[] nums3 = {1, 2};
int[] expected3 = {2, 1};
assertArrayEquals(expected3, solution.solve(nums3));
// Test case 4: Array with zeros
int[] nums4 = {0, 1, 2, 3};
int[] expected4 = {6, 0, 0, 0};
assertArrayEquals(expected4, solution.solve(nums4));
// Test case 5: Array with negative numbers
int[] nums5 = {-1, -2, -3, -4};
int[] expected5 = {-24, -12, -8, -6};
assertArrayEquals(expected5, solution.solve(nums5));
}
}

View File

@ -0,0 +1,39 @@
package com.hithomelabs.dsa.array.search_in_rotated_sorted_array;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case with rotation
int[] nums1 = {4, 5, 6, 7, 0, 1, 2};
int target1 = 0;
assertEquals(4, solution.solve(nums1, target1));
// Test case 2: Target not present
int[] nums2 = {4, 5, 6, 7, 0, 1, 2};
int target2 = 3;
assertEquals(-1, solution.solve(nums2, target2));
// Test case 3: Array with one element (target present)
int[] nums3 = {1};
int target3 = 1;
assertEquals(0, solution.solve(nums3, target3));
// Test case 4: Array with one element (target not present)
int[] nums4 = {1};
int target4 = 0;
assertEquals(-1, solution.solve(nums4, target4));
// Test case 5: Array is rotated at the pivot
int[] nums5 = {3, 1};
int target5 = 1;
assertEquals(1, solution.solve(nums5, target5));
// Test case 6: Array is not rotated
int[] nums6 = {1, 2, 3, 4, 5};
int target6 = 3;
assertEquals(2, solution.solve(nums6, target6));
}
}

View File

@ -0,0 +1,35 @@
package com.hithomelabs.dsa.array.three_sum;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SolutionTest {
@Test
void testSolve() {
Solution solution = new Solution();
// Test case 1: Normal case with positive numbers
int[] nums1 = {1, 2, -2, -1};
List<List<Integer>> expected1 = Arrays.asList(Arrays.asList(-2, -1, 1), Arrays.asList(-2, 1, 2));
assertEquals(expected1, solution.solve(nums1));
// Test case 2: No solution
int[] nums2 = {1, 2, 3};
List<List<Integer>> expected2 = new ArrayList<>();
assertEquals(expected2, solution.solve(nums2));
// Test case 3: Array with duplicates
int[] nums3 = {0, 0, 0, 0};
List<List<Integer>> expected3 = Arrays.asList(Arrays.asList(0, 0, 0));
assertEquals(expected3, solution.solve(nums3));
// Test case 4: Mixed positive and negative numbers
int[] nums4 = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> expected4 = Arrays.asList(Arrays.asList(-1, -1, 2), Arrays.asList(-1, 0, 1));
assertEquals(expected4, solution.solve(nums4));
// Test case 5: All elements are zero
int[] nums5 = {0, 0, 0};
List<List<Integer>> expected5 = Arrays.asList(Arrays.asList(0, 0, 0));
assertEquals(expected5, solution.solve(nums5));
}
}

View File

@ -0,0 +1,26 @@
package com.hithomelabs.dsa.array.two_sum;
import static org.junit.Assert.assertArrayEquals;
public class SolutionTest {
@Test
void testTwoSum() {
// Test case 1: nums = [2, 7, 11, 15], target = 9
int[] nums1 = {2, 7, 11, 15};
int target1 = 9;
int[] expected1 = {0, 1}; // indices of the numbers that add up to target
assertArrayEquals(expected1, new HashMapSolution().solve(nums1, target1));
// Test case 2: nums = [3, 2, 4], target = 6
int[] nums2 = {3, 2, 4};
int target2 = 6;
int[] expected2 = {1, 2}; // indices of the numbers that add up to target
assertArrayEquals(expected2, new HashMapSolution().solve(nums2, target2));
// Test case 3: nums = [3, 3], target = 6
int[] nums3 = {3, 3};
int target3 = 6;
int[] expected3 = {0, 1}; // indices of the numbers that add up to target
assertArrayEquals(expected3, new HashMapSolution().solve(nums3, target3));
}
}