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

Commit 941fb14

Browse files
committed
Added MissingRange and MergeInterval scratch
1 parent 2c31537 commit 941fb14

File tree

1 file changed

+55
-1
lines changed
  • CodingInterview/CodingInterview

1 file changed

+55
-1
lines changed

‎CodingInterview/CodingInterview/Tests.cs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,25 @@ string[] GetSummaryRanges(int [] inputArray)
150150
return listOfAnswers.ToArray();
151151
}
152152

153+
string[] GetMissingRanges(int lower, int upper, int[] inputArray)
154+
{
155+
if (inputArray == null || inputArray.Length == 0)
156+
throw new ArgumentNullException("InputArray needs to have elements");
157+
158+
if(lower > upper && lower >= 0 && upper >= 0)
159+
throw new ArgumentException("Correct arguments");
160+
161+
var negativeList = Enumerable.Range(lower, upper - lower + 1)
162+
.Where(element => !inputArray.Any(item => item == element));
163+
164+
return GetSummaryRanges(negativeList.ToArray());
165+
}
166+
167+
int[][] GetMergeIntervals(int[][] inputArray)
168+
{
169+
return inputArray;
170+
}
171+
153172
[Test]
154173
public void RemoveDuplicatesFromSortedArray_1()
155174
{
@@ -235,5 +254,40 @@ public void SummaryRanges_9()
235254
//then
236255
Assert.AreEqual(expectedArray, outputArray);
237256
}
238-
}
257+
258+
[Test]
259+
public void MissingRanges_10()
260+
{
261+
//given
262+
var inputArray = new int[] { 0, 1, 3, 50, 75 };
263+
int lower = 0;
264+
int upper = 99;
265+
var expectedArray = new string[] { "2", "4->49", "51->74", "76->99" };
266+
//when
267+
var outputArray = GetMissingRanges(lower, upper, inputArray);
268+
//then
269+
Assert.AreEqual(expectedArray, outputArray);
270+
}
271+
272+
[Test]
273+
public void MergeIntervals_11()
274+
{
275+
//given
276+
var inputArray = new int[][] {
277+
new int[] { 1, 3 },
278+
new int[] { 2, 6 },
279+
new int[] { 8, 10 },
280+
new int[] { 15, 18 }
281+
};
282+
var expectedArray = new int[][] {
283+
new int[] { 1, 6 },
284+
new int[] { 8, 10 },
285+
new int[] { 15, 18 }
286+
};
287+
//when
288+
var outputArray = GetMergeIntervals(inputArray);
289+
//then
290+
Assert.AreEqual(expectedArray, outputArray);
291+
}
292+
}
239293
}

0 commit comments

Comments
(0)

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