Skip to content

chapter 1 exercises 1,2,3 #2

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
wants to merge 3 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
13 changes: 13 additions & 0 deletions CrackingTheCodingInterview/Code/Chapter 1/Question1_1.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;

namespace Code
{
Expand Down Expand Up @@ -49,5 +50,17 @@ public static bool AreAllCharactersUniqueNoAdditionalMemory(string input)

return true;
}

// Space: O(n)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
// Space: O(n)
// Space: O(N)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I really, really, really, appreciate the feedback. I didn't expect it but it's great to learn from my mistakes.
I'll fix it as soon as I have enough time

// Time: O(1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Time: O(1)
// Time: O(N)

Distinct() requires an enumeration over the entire string so has an O(N) runtime

public static bool AreAllCharactersUniqueLinQSolution(string input)
{
if (string.IsNullOrEmpty(input))
{
return true;
}

return input.Distinct().ToArray().Length == input.Length;
}
}
}
25 changes: 25 additions & 0 deletions CrackingTheCodingInterview/Code/Chapter 1/Question1_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,30 @@ public static bool AreStringsPermutationNoSort(string string1, string string2)

return true;
}

// Space: O(N)
// Time: O(N)
public static bool AreStringPermutationsIntValues(string string1, string string2)
{
if (string.IsNullOrEmpty(string1) || string.IsNullOrEmpty(string2))
{
throw new ArgumentException("Input strings cannot be null or empty");
}

if (string1.Length != string2.Length)
{
return false;
}

int summyString1 = 0;
int summyString2 = 0;
for (int i = 0; i < string1.Length; i++)
{
summyString1 = summyString1 + string1[i];
summyString2 = summyString2 + string2[i];
}

return summyString1 == summyString2;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great idea, but unfortunately, it won't work in all cases :(

What you're doing is effectively creating a hash of the input and comparing to see if they're equal. This opens the opportunity for hash collisions.

Imagine a simplified example where characters have the following values:
A = 1
B = 2
C = 3
D = 4

The string ACC will have the following hash code:
A + C + C = 1 + 3 + 3 = 7

The string ABD will have the following hash code:
A + B + D = 1 + 2 + 4 = 7

These strings are going to be considered permutations using this algorithm, but in reality they're not permutations.

}
}
34 changes: 34 additions & 0 deletions CrackingTheCodingInterview/Code/Chapter 1/Question1_3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,39 @@ public static void ReplaceSpaces(char[] inputString, int length)
}
}
}

// Space: O(N)
// Time: O(N)
public static string ReplaceSpaces(char[] inputString)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works if we're looking for another string, but in this case the question is asking for us to update the string in the original char array.

{
if (inputString == null)
{
throw new ArgumentNullException(nameof(inputString), "Value cannot be null");
}

char[] outputString = new char[inputString.Length * 3];
int outputIndex = 0;
for (int i = 0; i < inputString.Length; i++)
{
if (char.IsWhiteSpace(inputString[i]))
{
outputString[outputIndex++] = '%';
outputString[outputIndex++] = '2';
outputString[outputIndex++] = '0';
}
else
{
outputString[outputIndex++] = inputString[i];
}
}

char[] finalString = new char[outputIndex];
for (int i = 0; i < outputIndex; i++)
{
finalString[i] = outputString[i];
}

return new string(finalString);
}
}
}
70 changes: 70 additions & 0 deletions Tests/Chapter 1/Test1_1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ public void ImplementationTwo_NoDuplicates_ReturnsTrue()
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationThree_NoDuplicates_ReturnsTrue()
{
// Arrange
const bool expected = true;
const string input = "abc";

// Act
var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input);

// Assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationOne_Duplicates_ReturnsFalse()
{
Expand Down Expand Up @@ -62,6 +76,20 @@ public void ImplementationTwo_Duplicates_ReturnsFalse()
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationThree_Duplicates_ReturnsFalse()
{
// Arrange
const bool expected = false;
const string input = "aba";

// Act
var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input);

// Assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationOne_CasingDifference_ReturnsTrue()
{
Expand Down Expand Up @@ -90,6 +118,20 @@ public void ImplementationTwo_CasingDifference_ReturnsTrue()
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationThree_CasingDifference_ReturnsTrue()
{
// Arrange
const bool expected = true;
const string input = "Aa";

// Act
var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input);

// Assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationOne_NullString_ReturnsTrue()
{
Expand Down Expand Up @@ -118,6 +160,20 @@ public void ImplementationTwo_NullString_ReturnsTrue()
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationThree_NullString_ReturnsTrue()
{
// Arrange
const bool expected = true;
const string input = null;

// Act
var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input);

// Assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationOne_EmptyString_ReturnsTrue()
{
Expand Down Expand Up @@ -145,5 +201,19 @@ public void ImplementationTwo_EmptyString_ReturnsTrue()
// Assert
Assert.AreEqual(expected, actual);
}

[TestMethod]
public void ImplementationThree_EmptyString_ReturnsTrue()
{
// Arrange
const bool expected = true;
var input = string.Empty;

// Act
var actual = Question1_1.AreAllCharactersUniqueLinQSolution(input);

// Assert
Assert.AreEqual(expected, actual);
}
}
}
4 changes: 4 additions & 0 deletions Tests/Chapter 1/Test1_2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public void BasicTest()
// Permutations
ValidateResult("abc", "abc", true);
ValidateResult("abc", "bca", true);
ValidateResult("abcdefg", "bcadefg", true);

// Not permutations
ValidateResult("abc", "abca", false);
ValidateResult("abc", "xyz", false);
ValidateResult("abc", "axa", false);
}

[TestMethod]
Expand Down Expand Up @@ -59,12 +61,14 @@ private static void ValidateResult(string str1, string str2, bool expectedResult
{
Assert.AreEqual(expectedResult, Question1_2.AreStringsPermutation(str1, str2));
Assert.AreEqual(expectedResult, Question1_2.AreStringsPermutationNoSort(str1, str2));
Assert.AreEqual(expectedResult, Question1_2.AreStringPermutationsIntValues(str1, str2));
}

private static void ValidateResult(string str1, string str2, Type expectedException)
{
TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringsPermutation(str1, str2); }, expectedException);
TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringsPermutationNoSort(str1, str2); }, expectedException);
TestHelpers.AssertExceptionThrown(() => { Question1_2.AreStringPermutationsIntValues(str1, str2); }, expectedException);
}
}
}
4 changes: 4 additions & 0 deletions Tests/Chapter 1/Test1_3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public void InvalidInputsTest()
// Null input string
str = null;
TestHelpers.AssertExceptionThrown(() => { Question1_3.ReplaceSpaces(str, 0); }, typeof(ArgumentNullException));

TestHelpers.AssertExceptionThrown(() => { Question1_3.ReplaceSpaces(str); }, typeof(ArgumentNullException));
}

private static void ValideResult(string input, string expectedResult, int? inputLength = null)
Expand All @@ -58,6 +60,8 @@ private static void ValideResult(string input, string expectedResult, int? input
{
Assert.AreEqual(expectedResult[i], str[i]);
}

Assert.AreEqual(Question1_3.ReplaceSpaces(str), expectedResult);
}
}
}