Skip to content
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

interview problems practice recursive #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions problems/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.interview</groupId>
<artifactId>problems</artifactId>
<version>1.0</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.24.2</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.interview.recursive.palindrome;

public record Palindrome(String input) {
private static final int STARTING_INDEX = 0;
private static final int OFFSET = 1;

public boolean check() {
if (input.isEmpty())
return true;
return isValid(STARTING_INDEX, endingIndex());

}

public boolean isValid(int startingIndex, int endIndex) {
if (startingIndex >= endIndex)
return true;
if (input.charAt(startingIndex) != input.charAt(endIndex))
return false;

return isValid(startingIndex + OFFSET, endIndex - OFFSET);

}

private int endingIndex() {
return input.length() - 1;
}
}
12 changes: 12 additions & 0 deletions problems/src/main/java/org/interview/recursive/palindrome/explain
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Given a string, write a recursive function that checks if the given string is a palindrome or not.

Examples:

Input : malayalam
Output : Yes
Reverse of malayalam is also
malayalam.

Input : max
Output : No
Reverse of max is not max.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.interview.recursive;

import org.assertj.core.api.Assertions;
import org.interview.recursive.palindrome.Palindrome;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class PalindromeShould {

@ParameterizedTest
@CsvSource(
{"ali , false",
"amin ,false",
"ddd , true",
"aia ,true",
"atda ,false",
"a ,true"})
void chick_is_or_not(String input, boolean expected) {
var palindrome = new Palindrome(input);
Assertions.assertThat(palindrome.check()).isEqualTo(expected);
}
}
Binary file not shown.
Binary file not shown.