ajaypathak
BAN USER@counterexample how u have generated these many test cases
- ajaypathak August 27, 2012public static void ReverseWordOfString(string s)
{
if (string.IsNullOrEmpty(s))
{
Console.WriteLine("String is empty");
return;
}
Stack<char> charStack = new Stack<char>();
var charArray = s.ToCharArray();
int length = charArray.Length;
Queue<char> result = new Queue<char>();
Console.WriteLine("input string : {0}", s);
for (int i = 0; i < length; i++)
{
if (charArray[i] != ' ')
{
charStack.Push(charArray[i]);
}
else
{
while (charStack.Count > 0)
{
result.Enqueue(charStack.Pop());
}
result.Enqueue(' ');
}
}
while (charStack.Count > 0)
{
result.Enqueue(charStack.Pop());
}
Console.Write("String After reversing the words : "); Console.Write(result.ToArray());
Console.WriteLine();
}
/// <summary>
/// 1--->2--->3--->4--->9--->8-->7--->6--->5--->10--->11--->NULL
/// </summary>
/// <param name="head"></param>
public static void correctList(Node<int> head)
{
if (head == null)
{
Console.WriteLine("Empty List");
return;
}
Node<int> current = head;
Node<int> first = null;
Node<int> last = null;
Node<int> mark1 = null;
while (current != null && current.NodeValue < current.NextNode.NodeValue)
{
mark1 = current;
current = current.NextNode;
}
first = current; //Pointer to node, LL Disorder starts
while (current != null && current.NodeValue > current.NextNode.NodeValue)
current = current.NextNode;
last = current.NextNode; //Pointer to node, LL Disorder Ends
current.NextNode = null; // Setting next of last node to null
Reverse(ref first);// reverse the sub linklist that dis order starts to disorder ends
mark1.NextNode = first; // join the intial linklist with reversed lkinked list
while (first.NextNode != null)
{
first = first.NextNode;
}
first.NextNode = last; // join the merged link list with sorted linklist
Console.WriteLine();
Console.WriteLine("Output");
PrintList(head);
Console.WriteLine();
}
public static void Reverse(ref Node<int> head)
{
Node<int> first;
Node<int> rest;
if (head == null) return;
first = head;
rest = first.NextNode;
if (rest == null) return;
Reverse(ref rest);
first.NextNode.NextNode = first;
first.NextNode = null;
head = rest;
}
public static void PrintList(Node<int> head)
{
while (head != null)
{
Console.Write(head.NodeValue);
head = head.NextNode;
if (head != null) Console.Write("->");
}
}
written following code which is using dictionary in o(n) time and space (n) times
public static void RepeatedEvenTimes(int[] inputArray)
{
Dictionary<int, int> numbers = new Dictionary<int, int>();
int length = inputArray.Length;
int num = -1;
for (int i = 0; i < length; i++)
{
if (numbers.Keys.Contains(inputArray[i]))
{
numbers[inputArray[i]] = numbers[inputArray[i]] + 1;
}
else
{
numbers.Add(inputArray[i], 1);
}
}
Console.Write("input Array ");
Console.WriteLine(string.Join(",", inputArray));
foreach (var item in numbers)
{
if (item.Value % 2 == 0)
{
num = item.Key;
Console.WriteLine("{0} Repeated {1} times ", item.Key, item.Value);
break;
}
}
if (num == -1)
{
Console.WriteLine("No number Repeated even number of times");
}
Console.WriteLine("-----------------------------------------------");
}
public static string ConvertToAlphabet(int number)
{
int temp = number;
char[] alphabets = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string Alphabet = string.Empty;
int Index = 0;
if (number <= 0)
{
Console.WriteLine("input number {0} is invalid", temp);
return string.Empty;
}
if (number < 27)
{
Console.WriteLine("input number {0} , output alphabet {1}", temp, alphabets[number - 1]);
return alphabets[number - 1].ToString();
}
while (number > 0)
{
Index = number % 26;
number = number / 26; ;
if (Index == 0)
{
Index = 26;
number = number - 1;
}
Alphabet = alphabets[Index - 1] + Alphabet;
}
Console.WriteLine("input number {0} , output alphabet {1}", temp, Alphabet);
return Alphabet;
}
input
Microsoft.ConvertToAlphabet(-11);
Microsoft.ConvertToAlphabet(1);
Microsoft.ConvertToAlphabet(26);
Microsoft.ConvertToAlphabet(52);
Microsoft.ConvertToAlphabet(28);
Microsoft.ConvertToAlphabet(702);
Microsoft.ConvertToAlphabet(703);
output
input number -11 is invalid
input number 1 , output alphabet A
input number 26 , output alphabet Z
input number 52 , output alphabet AZ
input number 28 , output alphabet AB
input number 702 , output alphabet ZZ
input number 703 , output alphabet AAA
public static int GetBalancedIndex(int[] inputArray)
{
Int64 arraySum = 0;
Int64 leftSumt = 0;
for (int i = 0; i < inputArray.Length; i++)
{
Console.Write(inputArray[i] + " , ");
arraySum += inputArray[i];
}
Console.WriteLine();
for (int i = 0; i < inputArray.Length; i++)
{
leftSumt += inputArray[i];
arraySum -= inputArray[i];
if (arraySum == leftSumt)
{
Console.WriteLine("Balanced Index {0} : ", i);
return i;
}
}
return -1;
}
The question said “Suppose you are developing a code editor” so if this is the case then we can use following path.
Create a list of function also
Create a hashmap which will have function name and a bool parameter say reachable.
So whenever you are writing code and encounter any other function, just add that function to hashmap.
And while compiling the application, return the list of functions which are not present in hashmap.
If code files are already given, then we have to scan the entire code
Creating a max heap using the numbers and if a negative number comes take the mod
Then apply heap sort only for first three elements.
The product of first three element will be the maximum
first reverse the string and than each word.
- ajaypathak July 11, 2012public static void Reverse(ref Node<int> head)
{
Node<int> first;
Node<int> rest;
if (head == null) return;
first = head;
rest = first.NextNode;
if (rest == null) return;
Reverse(ref rest);
first.NextNode.NextNode = first;
first.NextNode = null;
head = rest;
}
- ajaypathak July 11, 2012public static void TargetSum(int[] array, int target)
{
Dictionary<int, int> hashMap = new Dictionary<int, int>();
int lookup = 0;
for (int i = 0; i < array.Length; i++)
{
lookup = target - array[i];
if (hashMap.ContainsKey(lookup) == true)
{
Console.WriteLine("Pair {0},{1}", lookup, array[i]);
}
else
{
hashMap.Add(array[i], array[i]);
}
}
}
you need to play with the value of toggle,
what I am seeing here is u r just decreasing the value of toggle, when ever controls comes into else block
if i understood the question correctly, we have to find the list of common friends.
this problem is exactly the same as, finding common elements in two arrays or list
this will not give the correct results
it will give
original array
1 , 2 , 3 , 4 , 5 ,
output
Pair 5,1
Pair 4,2
Pair 3,3
Pair 2,4
Pair 1,5
i think, we have to remove the items from hasmap , as soon a key is found in hashmap
en.wikipedia.org/wiki/Counting_sort
- ajaypathak June 28, 2012int[] input = new int[10000];
Random random = new Random();
for (int i=0;i<1000;i++){
input[i]=random.Next(0, 1024);
}
Sorting.CountingSort(input);
public static void CountingSort(int[] arrayA)
{
Console.WriteLine("Original Array");
for (int i = 0; i < arrayA.Length; i++)
{
Console.Write(arrayA[i] + " , ");
}
Console.WriteLine("");
int k = 1024;
int[] arrayB = new int[arrayA.Length];
int[] arrayC = new int[k];
for (int i = 0; i < arrayC.Length; i++)
{
arrayC[i] = 0;
}
for (int j = 0; j < arrayA.Length; j++)
{
arrayC[arrayA[j]] = arrayC[arrayA[j]] + 1;
}
//Place the number of elements less than each value at i into array C.
for (int i = 1; i < k; i++)
arrayC[i] = arrayC[i] + arrayC[i - 1];
//Place each element of arrayA into its correct sorted position in the
//output array B.
for (int j = arrayA.Length - 1; j >= 0; j--)
{
arrayB[arrayC[arrayA[j]] - 1] = arrayA[j];
arrayC[arrayA[j]] = arrayC[arrayA[j]] - 1;
}
//Overwrite the original arrayA with the output arrayB.
Console.WriteLine("Sorted Array");
for (int i = 0; i < arrayA.Length; i++)
{
arrayA[i] = arrayB[i];
Console.Write(arrayA[i] + " , ");
}
Console.WriteLine("");
}
if array has only one element than we can throw a exception or print a message saying that array has only one element
public static int FindSecondNdLarge(int[] array)
{
int size = array.Length;
int[] max = new int[] { 0, 0 };
int counter;
if (array[0] > array[1])
{
max[0] = array[0];
max[1] = array[0];
}
else
{
max[0] = array[1];
max[1] = array[0];
}
for (counter = 2; counter < size; counter++)
{
if (array[counter] > max[1])
{
if (array[counter] > max[0])
{
max[1] = max[0];
max[0] = array[counter];
}
else
{
max[1] = array[counter];
}
}
}
return max[1];
}
- ajaypathak June 20, 2012
string inputString = "PST456DA85M2A!!23++4600";
- ajaypathak September 01, 2012char[] inputChars = inputString.ToCharArray();
double sum = 0;
double tempnum = 0;
double powerCounter = 0;
double num;
for (int i = inputChars.Length - 1; i >= 0; i--)
{
if (inputChars[i] >= 48 && inputChars[i] <= 57)
{
num = inputChars[i] - 48;
tempnum = num * Math.Pow(10, powerCounter) + tempnum;
powerCounter = powerCounter + 1;
}
else
{
sum = sum + tempnum;
powerCounter = 0;
tempnum = 0;
}
}
Console.WriteLine();
Console.WriteLine(inputString);
Console.WriteLine(sum);
Console.ReadLine();