Bhavya

Code -> Break -> Fix -> Blog


Leave a comment

Mettl Coding Test : Find the max. distance between two occurrences of a char in a string

Problem Statement:

  1. Write a method to find the max. distance between two occurrences of a char in a string.
  2. Distance is the no. of unique characters (excluding whitespaces and searched character) between two occurrences of the searched character.

Example 1:

SearchLetter(“my name is granar”, “a”);

Output : 7 (i.e. m,e,i,s,g,r,n)

Example 2:

SearchLetter(“the capital of minnesota is saint paul”, “y”);

Output : -1 (no. occurrence of “y”)

Example 3:

SearchLetter(“quick brown fox jumps over the lazy dog”, “a”);

Output : -1 (only 1 occurrence of “a”)

Solution

public static int output1;

public static void SearchLetter(string input1, string input2)
{
if (string.IsNullOrEmpty(input1)
|| string.IsNullOrEmpty(input2)
|| input1.Count() < 2
|| input2.Count() != 1)
{
output1 = -1;
return;
}

var firstIndex = input1.IndexOf(input2);
var lastIndex = input1.LastIndexOf(input2);

if (firstIndex == lastIndex)
{
output1 = -1;
return;
}

var distance = lastIndex - firstIndex;
var finalSearchString = input1.Substring(firstIndex + 1, distance - 1);

finalSearchString = finalSearchString.Replace(" ", string.Empty)
.Replace(input2, string.Empty);

string tempString = string.Empty;
string finalString = string.Empty;

foreach (var value in finalSearchString)
{
if (tempString.IndexOf(value) == -1)
{
tempString += value;
finalString += value;
}
}

output1 = finalString.Count();
}

~BJS
 

Advertisements


Leave a comment

Mettl Coding Test : List the duplicate integers in the array

Problem Statement

  1. Create a method which will list out the duplicate entries in a given integer array.
  2. The input array should have only positive integers.
  3. The method should take 2 parameters, 1st is the count of the array and 2nd the actual array
  4. If no duplicate entries are found then return {-1}.

Example 1 :

DuplicateArray(6, new int[] { 4, 4, 7, 8, 8, 9 });

Output={4,8}

Example 2:

DuplicateArray(6, new int[] { 4, 4, -7, 8, 8, 9 });

Output={-1}

Example 3:

DuplicateArray(7, new int[] { 3, 6, 5, 7, 8, 19, 32 });

Output={-1}

 

Soultion

public static int[] output1;

public static void DuplicateArray(int input1, int[] input2)
{
// Validate input1 as it should match with input2.count
// Validate : more than 1 integer present in the array.
// Validate : Positive integers
if (input1 != input2.Count() || input1 &lt; 1
|| input2.FirstOrDefault(x =&gt; x &lt; 0) &lt; 0)
{
output1 = new int[] { -1 };
return;
}

List&lt;int&gt; listDuplicateValues = new List&lt;int&gt;();

// Group by numbers and check the count for duplicate entries.
var groups = input2.GroupBy(x =&gt; x);
foreach (var group in groups)
{
if (group.Count() &gt; 1)
{
listDuplicateValues.Add(group.Key);
}
}

// Validate : if list is empty then set output1 to -1
output1 = listDuplicateValues.Count == 0 ?
new int[] { -1 } : output1 = listDuplicateValues.ToArray();
}

~BJS