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
 


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


Leave a comment

Print the alphabets which are not part of a given string (C#)

Problem Statement: Print the alphabets which are not part of a given string.

Solution


var alpha = new Dictionary&lt;char, bool&gt;();
alpha.Add('a', false);
alpha.Add('b', false);
alpha.Add('c', false);
alpha.Add('d', false);
alpha.Add('e', false);
alpha.Add('f', false);
alpha.Add('g', false);
alpha.Add('h', false);
alpha.Add('i', false);
alpha.Add('j', false);
alpha.Add('k', false);
alpha.Add('l', false);
alpha.Add('m', false);
alpha.Add('n', false);
alpha.Add('o', false);
alpha.Add('p', false);
alpha.Add('q', false);
alpha.Add('r', false);
alpha.Add('s', false);
alpha.Add('t', false);
alpha.Add('u', false);
alpha.Add('v', false);
alpha.Add('w', false);
alpha.Add('x', false);
alpha.Add('y', false);
alpha.Add('z', false);

var inputString = &quot;bhavyajshah&quot;;

foreach(char ch in inputString.ToLower())
alpha[ch] = true;

foreach (var item in alpha)
{
if (item.Value != true)
Console.WriteLine(item.Key);
}


Leave a comment

List duplicates in a given Binary Search Tree

Problem : List all duplicates in a given Binary Search Tree

Assumptions :

1. All “less than” values goes to the LEFT.

2. All “greater than or equal to” values goes to the RIGHT.

List : 5, 3, 10, 5, 4, 3, 7, 5, 5, 5, 2, 4, 10, 8, 10, 5

Output : 3, 4, 5, 5, 5, 5, 5, 10, 10


internal class BST
{
public int Data;
public BST Left;
public BST Right;
}

internal class Program
{
static int lastData;

public static void Main()
{
var root = BuildTree(5, 3, 10, 5, 4, 3, 7, 5, 5, 5, 2, 4, 10, 8, 10, 5);

Console.WriteLine("Duplicate List");
ListDuplicate(root);

Console.ReadLine();
}

private static BST BuildTree(params int[] list)
{
BST temp = null;
BST root = null;

foreach (var l in list)
{
temp = root;
BST node = new BST();
node.Data = l;
node.Left = null;
node.Right = null;

if (temp == null)
{
temp = node;
root = node;
}
else
{
while (temp != null)
{
if (l < temp.Data)
{
if (temp.Left == null)
{
temp.Left = node;
break;
}

temp = temp.Left;
}
else
{
if (temp.Right == null)
{
temp.Right = node;
break;
}

temp = temp.Right;
}
}
}
}

return root;
}

private static void ListDuplicate(BST root)
{
if (root == null)
{
return;
}

ListDuplicate(root.Left);

if (lastData == root.Data)
{
Console.WriteLine(root.Data);
}

lastData = root.Data;

ListDuplicate(root.Right);

}
}

~BS


Leave a comment

Codility Problem

Problem Statement :

Divide the array such that the no. of occurrences of X element on one side should be equal to the no. of occurrences of non-X elements in the other half.

Example:

Array : [1,5,6,2,8,5] and X=5

Can be divided into two arrays i.e [1,5,6,2] and [8,5] such that no. of occurrences of 5 in the 1st array i.e. 1 is equal to no. of occurrences of non-5 elements in the 2nd arrya i.e.1.

So the function should return the index of 8 i.e. 4.

Solution


public static int solution(int X, int[] A)
{
int leftSame = 0;
int rightSame = 0;
int leftDiff = 0;
int rightDiff = 0;
int index = 0;

for (int i = 0; i < A.Length; i++)
{
if (A[i] == X)
{
leftSame += 1;
}
else
{
leftDiff += 1;
}

if (i > ((A.Length - 1) - i))
{
break;
}

if (A[(A.Length - 1) - i] == X)
{
rightSame += 1;
}
else
{
rightDiff += 1;
}

if (leftSame == rightDiff)
{
index = i;
}

if (leftDiff == rightSame)
{
index = A.Length - 1 - i;
}

}

return index;
}


Leave a comment

Codility Problem : Find max deviation index

Problem statement:

For a given array say [1,-5,8,3,-10,3,5,8] find the index which is far-most from the avg.

Answer : 4 i.e. -10 is far-most from the average i.e. 1.75.

Solution:


public static int solution3(double[] arr)
{
int max = 0;
int min = 0;
double avg = arr[0];

if (arr.Length == 0)
{
return -1;
}

for (int i = 0; i < arr.Length; i++)
{
if (arr[i] < arr[min])
{
min = i;
}

if (arr[i] > arr[max])
max = i;

avg += arr[i];
}

avg = avg / arr.Length;

var a1 = avg - arr[min];
var a2 = avg - arr[max];

if (Math.Abs(a1) > Math.Abs(a2))
{
return min;
}
else
{
return max;
}
}


1 Comment

Petrol Pump (Gas station) Problem (C#)

Problem Description:

We have a group of Petrol pump on a Circular Road (Ring Road) say P(1), P(2), P(3)…P(N).

Each Petrol pump has some amount of fuel with it say P(1) has F(1), P(2) has F(2) …. P(N) has F(N).

Distance between two consecutive Petrol pumps are D(1), D(2), D(3) … D(N).So distance between P(1) and P(2) is D(1) and P(2) and P(3) is D(2)…. P(N) and P(1) is D(N).

Find the starting point such that the vehicle should be make the full circle.


static int GetStartPoint(int[] fuel, int[] distance)
{
if (fuel == null || distance == null || fuel.Length == 0 || distance.Length == 0)
{
//invalid input.
return -1;
}

if (fuel.Sum() < distance.Sum())
{
// No StartPoint found.
return -2;
}

int startPoint = 0;
int pumpLength = fuel.Length - 1;

int leftOverFuel = 0;
int i = 0;

do
{
if (i > pumpLength)
{
// No StartPoint found.
return -2;
}

if (fuel[i] + leftOverFuel < distance[i])
{
startPoint = i++;
}
else
{
leftOverFuel += fuel[i] - distance[i];
if (i == pumpLength)
{
i = 0;
}
else
{
i++;
}
}
} while (i != startPoint);

return startPoint + 1;
}