├── CodingPractice
├── CodingPractice.sln
└── CodingPractice
│ ├── Backtracking
│ └── NQueen.cs
│ ├── CodingPractice.csproj
│ ├── CodingPractice.csproj.user
│ ├── Coding_Exercises
│ ├── Boggle_Trie.cs
│ ├── Count_number_of_ways_to_partition_a_set.cs
│ ├── Factorial.cs
│ ├── Fibonacci.cs
│ ├── Find_a_Pair_Of_Sum.cs
│ ├── Finding_all_subsets_ of_a_given_set.cs
│ ├── KNearestPoint.cs
│ ├── LRU_Cache_Least_Recently_Used.cs
│ ├── Maximum_Subarray_Sum.cs
│ ├── Partition_problem_Sub_Set.cs
│ ├── PowerOf.cs
│ ├── Power_Set.cs
│ ├── Search_Word_2D_Grid.cs
│ ├── Stair_Case.cs
│ ├── largest_sum_of_non_adjacent_numbers.cs
│ ├── print_matrix_spiral_form.cs
│ └── the_celebrity_problem.cs
│ ├── DBMS
│ └── README.md
│ ├── DataStructure
│ ├── AVL
│ │ └── AvlTree.cs
│ ├── Array
│ │ └── README.md
│ ├── BinarySearchTree
│ │ ├── BinarySearch.cs
│ │ └── BinaryTree.cs
│ ├── DFS
│ │ ├── BFS_1.cs
│ │ ├── DFS_1.cs
│ │ └── dfs._bfs.cs
│ ├── DoublyLinkedList
│ │ ├── DoublyLinkedList.cs
│ │ └── README.md
│ ├── HashTable
│ │ ├── HashTableImp.cs
│ │ └── README.MD
│ ├── Heap
│ │ ├── MaxHeap.cs
│ │ ├── MinHeap.cs
│ │ └── README.MD
│ ├── LinkedList
│ │ ├── LinkedList.cs
│ │ └── Readme.md
│ ├── Queue
│ │ ├── QueueImp.cs
│ │ └── README.MD
│ ├── Stack
│ │ ├── README.MD
│ │ ├── StackImp.cs
│ │ └── Stack_LinkedList.cs
│ ├── Trees
│ │ ├── BinarySearchTree.cs
│ │ ├── BinaryTree.cs
│ │ ├── README.MD
│ │ ├── TreeNode.cs
│ │ └── readme1.md
│ ├── Trie
│ │ ├── README.MD
│ │ └── TrieNode.cs
│ └── bin
│ │ └── Debug
│ │ └── netcoreapp2.0
│ │ ├── CodingPractice.deps.json
│ │ ├── CodingPractice.dll
│ │ ├── CodingPractice.pdb
│ │ ├── CodingPractice.runtimeconfig.dev.json
│ │ └── CodingPractice.runtimeconfig.json
│ ├── DesignPattern
│ ├── AbstractFactory.cs
│ ├── AdapterPattern.cs
│ ├── Builder.cs
│ ├── Facade.cs
│ ├── FactoryDesign.cs
│ ├── ObserverPattern.cs
│ └── README.md
│ ├── GeeksForGeeks
│ ├── 3Sum.cs
│ ├── AddBinary.cs
│ ├── AlienLanguage.cs
│ ├── Anagram-Substring-Search.cs
│ ├── BadVersion.cs
│ ├── BestTimeToBuyAndSellStock.cs
│ ├── Binary-Tree-Maximum-Path-Sum.cs
│ ├── ClosesTwoPoint.cs
│ ├── Coin_Change.cs
│ ├── FindPeekElement.cs
│ ├── FlatternBST.cs
│ ├── IntegerToWords.cs
│ ├── Is_Heap.cs
│ ├── LetterCombinationOfPhoneNumber.cs
│ ├── MinJump.cs
│ ├── Min_Heap.cs
│ ├── MoveZero.cs
│ ├── OneEditDistance.cs
│ ├── Palindrom2.cs
│ ├── ProductOfArrayExceptSelf.cs
│ ├── Random-Pick-With-Weight.cs
│ ├── RemoveInvalidParentheses.cs
│ ├── SerializeAndDeSerializeBST.cs
│ ├── Subtree-of-another-tree.cs
│ ├── TreeNode.cs
│ ├── UnionIntersection.cs
│ ├── ValidPalindrome.cs
│ ├── WordBreak.cs
│ ├── Work-Break-2.cs
│ └── minimum-remove-to-make-valid-parentheses.cs
│ ├── GraphAlgorithm
│ ├── Graph.cs
│ └── README.md
│ ├── HackerRank
│ ├── 2DArray_DS.cs
│ ├── AlternatingCharacters.cs
│ ├── Apple_and_Orange.cs
│ ├── ArrayManipulation.cs
│ ├── BubbleSort.cs
│ ├── CallCenter
│ │ └── Employee.cs
│ ├── Circular_Array_Rotation_and_GCD_HCM.cs
│ ├── ClosestCommonManager.cs
│ ├── CommonChild.cs
│ ├── Compare_the_Triplets.cs
│ ├── Count_Triplets.cs
│ ├── Diagonal_Difference.cs
│ ├── FraudulentActivityNotifications.cs
│ ├── Grading_Students.cs
│ ├── GridChallenge.cs
│ ├── Insert_a_node_at_the_head_of_a_linked_list.cs
│ ├── IsThisABinarySearchTree.cs
│ ├── LeftRotation.cs
│ ├── LuckBalance.cs
│ ├── MarcsCakewalk.cs
│ ├── MarkAndToys.cs
│ ├── MaximumPerimeterTriangle.cs
│ ├── Mini_Max_Sum.cs
│ ├── MinimumSwaps2.cs
│ ├── Minimum_Absolute_Difference_in_an_Array.cs
│ ├── NewYearChaos.cs
│ ├── OpenCloseSymbolsCheck.cs
│ ├── Plus_Minus.cs
│ ├── Print_the_Elements_of_a_Linked_List.cs
│ ├── RansomNote.cs
│ ├── SherlockAndAnagrams.cs
│ ├── SherlockAndTheValidString.cs
│ ├── SliceDown.cs
│ ├── SpecialPalindromeAgain.cs
│ ├── Staircase.cs
│ ├── TheMaximumSubarray.cs
│ ├── Time_Conversion.cs
│ ├── TwoStrings.cs
│ └── WordExclude.cs
│ ├── Leetcode
│ └── Paint-House.cs
│ ├── OnlineProblem
│ ├── CareerCup.cs
│ └── Glassdoor.cs
│ ├── Practice.cs
│ ├── Program.cs
│ ├── Solution.cs
│ ├── SortingAlgorithm
│ ├── AmazonDemo.cs
│ ├── BubbleSort.cs
│ ├── HeapSort.cs
│ ├── InsertionSort.cs
│ ├── Lexicographical.cs
│ ├── MergeArrayWithOutExtraSpace.cs
│ ├── MergeSort.cs
│ ├── MergeTwoArray.cs
│ ├── QuickSort.cs
│ ├── README.MD
│ └── SelectionSort.cs
│ ├── StringManipulation
│ └── StringManipulations.cs
│ ├── bin
│ └── Debug
│ │ └── netcoreapp2.0
│ │ ├── CodingPractice.deps.json
│ │ ├── CodingPractice.dll
│ │ ├── CodingPractice.pdb
│ │ ├── CodingPractice.runtimeconfig.dev.json
│ │ └── CodingPractice.runtimeconfig.json
│ └── obj
│ ├── CodingPractice.csproj.nuget.cache
│ ├── CodingPractice.csproj.nuget.g.props
│ ├── CodingPractice.csproj.nuget.g.targets
│ ├── Debug
│ └── netcoreapp2.0
│ │ ├── CodingPractice.AssemblyInfo.cs
│ │ ├── CodingPractice.AssemblyInfoInputs.cache
│ │ ├── CodingPractice.csproj.CoreCompileInputs.cache
│ │ ├── CodingPractice.csproj.FileListAbsolute.txt
│ │ ├── CodingPractice.csprojResolveAssemblyReference.cache
│ │ ├── CodingPractice.dll
│ │ └── CodingPractice.pdb
│ └── project.assets.json
├── Images
├── Binary_tree.png
├── Circularly-linked-list.png
├── Doubly-linked-list.png
├── Justsearch.gif
├── SQLNOSQL.png
├── Singly-linked-list.png
├── Test
├── adj.jpg
├── binary_search_tree.png
├── bt_types.JPG
├── digraph.png
├── directed.jpg
├── doubly-linked-list2.png
├── graph-ex1.jpeg
├── graph.JPG
├── graphtypes.png
├── imp-tree.jpg
├── linkedlist.JPG
├── mat.jpg
├── minheap.jpg
├── osi-model.png
├── tree-parts.jpg
├── tree_traversal.png
├── trees.jpg
├── trie.jpeg
├── undirected.jpg
└── weighted.jpg
└── README.md
/CodingPractice/CodingPractice.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27130.2010
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodingPractice", "CodingPractice\CodingPractice.csproj", "{6794B2EF-222F-48A6-AC01-1F37CFEFBCD2}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {6794B2EF-222F-48A6-AC01-1F37CFEFBCD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {6794B2EF-222F-48A6-AC01-1F37CFEFBCD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {6794B2EF-222F-48A6-AC01-1F37CFEFBCD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {6794B2EF-222F-48A6-AC01-1F37CFEFBCD2}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {6844284E-C6D0-4F60-883B-D4FBBF730AF4}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Backtracking/NQueen.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.GeeksForGeeks
8 | {
9 | public class NQueen
10 | {
11 | static int N = 4;
12 |
13 | public static bool solveNQ()
14 | {
15 | int[][] board = {new int[] {0, 0, 0, 0},
16 | new int[] {0, 0, 0, 0},
17 | new int[] {0, 0, 0, 0},
18 | new int[] {0, 0, 0, 0}
19 | };
20 |
21 |
22 | return NQueenUtil(board, 0);
23 | }
24 | private static bool isSafe(int[][] board, int row, int col)
25 | {
26 | int i, j;
27 |
28 | /* Check this row on left side */
29 | for (i = 0; i < col; i++)
30 | if (board[row][i] == 1)
31 | return false;
32 |
33 | /* Check upper diagonal on left side */
34 | for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
35 | if (board[i][j] == 1)
36 | return false;
37 |
38 | /* Check lower diagonal on left side */
39 | for (i = row, j = col; j >= 0 && i < N; i++, j--)
40 | if (board[i][j] == 1)
41 | return false;
42 |
43 | return true;
44 | }
45 |
46 | private static bool NQueenUtil(int[][] board, int col)
47 | {
48 | if (col >= N)
49 | return true;
50 |
51 | /* Consider this column and try placing
52 | this queen in all rows one by one */
53 | for (int i = 0; i < N; i++)
54 | {
55 | /* Check if the queen can be placed on
56 | board[i][col] */
57 | if (isSafe(board, i, col))
58 | {
59 | /* Place this queen in board[i][col] */
60 | board[i][col] = 1;
61 |
62 | /* recur to place rest of the queens */
63 | if (NQueenUtil(board, col + 1) == true)
64 | return true;
65 |
66 |
67 | board[i][col] = 0; // BACKTRACK
68 | }
69 | }
70 |
71 | /* If the queen can not be placed in any row in
72 | this colum col, then return false */
73 | return false;
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/CodingPractice.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/CodingPractice.csproj.user:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | true
5 |
6 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Count_number_of_ways_to_partition_a_set.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | /*
8 | Input: n = 3, k = 2
9 | Output: 3
10 | Explanation: Let the set be {1, 2, 3}, we can partition
11 | it into 2 subsets in following ways
12 | {{1,2}, {3}}, {{1}, {2,3}}, {{1,3}, {2}}
13 |
14 | Input: n = 3, k = 1
15 | Output: 1
16 | Explanation: There is only one way {{1, 2, 3}}
17 | S(n, k) = k*S(n-1, k) + S(n-1, k-1)
18 | */
19 | class Count_number_of_ways_to_partition_a_set
20 | {
21 | #region recursive
22 | // Returns count of different
23 | // partitions of n elements in
24 | // k subsets
25 | //The time complexity of above recursive solution is exponential.
26 | //The solution can be optimized as there are overlapping subproblems
27 | public static int countP(int n, int k)
28 | {
29 |
30 | // Base cases
31 | if (n == 0 || k == 0 || k > n)
32 | return 0;
33 | if (k == 1 || k == n)
34 | return 1;
35 |
36 | // S(n+1, k) = k*S(n, k) + S(n, k-1)
37 | return (k * countP(n - 1, k)
38 | + countP(n - 1, k - 1));
39 | }
40 | #endregion
41 | #region overlapping
42 | // Returns count of different partitions of n
43 | // elements in k subsets
44 | //Time Complexity: O(n x k)
45 | //Auxiliary Space: O(n x k)
46 | static int countP_over(int n, int k)
47 | {
48 | // Table to store results of subproblems
49 | int[,] dp = new int[n + 1, k + 1];
50 |
51 | // Base cases
52 | for (int i = 0; i <= n; i++)
53 | dp[i, 0] = 0;
54 | for (int i = 0; i <= k; i++)
55 | dp[0, k] = 0;
56 |
57 | // Fill rest of the entries in dp[][]
58 | // in bottom up manner
59 | for (int i = 1; i <= n; i++)
60 | for (int j = 1; j <= k; j++)
61 | if (j == 1 || i == j)
62 | dp[i, j] = 1;
63 | else
64 | dp[i, j] = j * dp[i - 1, j] + dp[i - 1, j - 1];
65 |
66 | return dp[n, k];
67 |
68 | }
69 | #endregion
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Factorial.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | public class Factorial
8 | {
9 | /***
10 | * https://www.geeksforgeeks.org/factorial-large-number/
11 | *
12 | ***/
13 |
14 | static int factorial(int n)
15 | {
16 | int res = 1, i;
17 | for (i = 2; i <= n; i++)
18 | res *= i;
19 | return res;
20 | }
21 |
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Fibonacci.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | class Fibonacci
8 | {
9 | /* simple recursive program for Fibonacci numbers */
10 | int fib(int n)
11 | {
12 | if (n <= 1)
13 | return n;
14 | return fib(n - 1) + fib(n - 2);
15 | }
16 |
17 | #region Memorization (Top down)
18 | static int MAX = 100;
19 | static int NIL = -1;
20 | static int[] lookup = new int[MAX];
21 |
22 | /* Function to initialize NIL
23 | values in lookup table */
24 | static void initialize()
25 | {
26 | for (int i = 0; i < MAX; i++)
27 | lookup[i] = NIL;
28 | }
29 | /* function for nth Fibonacci number */
30 | ///
31 | /// Memoization (Top Down): The memoized program for a problem is similar to the recursive version with a small
32 | /// modification that it looks into a lookup table before computing solutions. We initialize a
33 | /// lookup array with all initial values as NIL. Whenever we need the solution to a subproblem,
34 | /// we first look into the lookup table. If the precomputed value is there then we return that value,
35 | /// otherwise, we calculate the value and put the result in the lookup table so that it can be reused later
36 | ///
37 | ///
38 | ///
39 | static int fib_mem(int n)
40 | {
41 | if (lookup[n] == NIL)
42 | {
43 | if (n <= 1)
44 | lookup[n] = n;
45 | else
46 | lookup[n] = fib_mem(n - 1) + fib_mem(n - 2);
47 | }
48 | return lookup[n];
49 | }
50 |
51 | #endregion
52 |
53 | #region Tabulation (Bottom Up):
54 | ///
55 | /// The tabulated program for a given problem builds a table in bottom up fashion and returns the
56 | /// last entry from table. For example, for the same Fibonacci number, we first calculate fib(0)
57 | /// then fib(1) then fib(2) then fib(3) and so on. So literally, we are building
58 | /// the solutions of subproblems bottom-up.
59 | ///
60 | ///
61 | ///
62 | static int fib_tab(int n)
63 | {
64 | int[] f = new int[n + 1];
65 | f[0] = 0;
66 | f[1] = 1;
67 | for (int i = 2; i <= n; i++)
68 | f[i] = f[i - 1] + f[i - 2];
69 | return f[n];
70 | }
71 | #endregion
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Find_a_Pair_Of_Sum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | class Find_a_Pair_Of_Sum
8 | {
9 | #region Method one time O(nLogn)
10 | /*
11 | Time Complexity: Depends on what sorting algorithm we use. If we use Merge Sort or
12 | Heap Sort then (-)(nlogn) in worst case. If we use Quick Sort then O(n^2) in worst case.
13 | Auxiliary Space : Again, depends on sorting algorithm. For example auxiliary space is O(n)
14 | for merge sort and O(1) for Heap Sort.
15 | */
16 | static bool hasArrayTwoCandidates(int[] A,
17 | int sum)
18 | {
19 | int l, r;
20 |
21 | /* Sort the elements */
22 | Array.Sort(A);
23 |
24 | /* Now look for the two candidates
25 | in the sorted array*/
26 | l = 0;
27 | r = A.Length - 1;
28 | while (l < r)
29 | {
30 | if (A[l] + A[r] == sum)
31 | return true;
32 | else if (A[l] + A[r] < sum)
33 | l++;
34 | else // A[i] + A[j] > sum
35 | r--;
36 | }
37 | return false;
38 | }
39 | #endregion
40 |
41 | #region Method 2 O(n) using hash
42 | ///
43 | /// Time Complexity: O(n)
44 | ///Auxiliary Space: O(n) where n is size of array.
45 | ///
46 | ///
47 | ///
48 | static void printpairs(int[] arr,
49 | int sum)
50 | {
51 |
52 | HashSet s = new HashSet();
53 | for (int i = 0; i < arr.Length; ++i)
54 | {
55 | int temp = sum - arr[i];
56 |
57 | // checking for condition
58 | if (temp >= 0 && s.Contains(temp))
59 | {
60 | Console.Write("Pair with given sum " +
61 | sum + " is (" + arr[i] +
62 | ", " + temp + ")");
63 | }
64 | s.Add(arr[i]);
65 | }
66 | }
67 | #endregion
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Finding_all_subsets_ of_a_given_set.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | ///
8 | /// https://www.geeksforgeeks.org/finding-all-subsets-of-a-given-set-in-java/
9 | /// S = {a, b, c, d}
10 | /// {}, {a} , {b}, {c}, {d}, {a,b}, {a,c},
11 | ///{a,d}, {b,c}, {b,d}, {c,d}, {a,b,c},
12 | ///{a,b,d}, {a,c,d}, {b,c,d}, {a,b,c,d}
13 | ///The total number of subsets of any given set is equal to 2^ (no. of elements in the set).
14 | ///If we carefully notice it is nothing but binary numbers from 0 to 15 which can be shown as below:
15 | ///
16 | class Finding_all_subsets__of_a_given_set
17 | {
18 | static void printSubsets(char[] set)
19 | {
20 | int n = set.Length;
21 |
22 | // Run a loop for printing all 2^n
23 | // subsets one by obe
24 | for (int i = 0; i < (1 << n); i++)
25 | {
26 | //System.out.print("{ ");
27 |
28 | // Print current subset
29 | for (int j = 0; j < n; j++)
30 |
31 | // (1< 0) {
37 | // System.out.print(set[j] + " ");
38 | }
39 |
40 | // System.out.println("}");
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Partition_problem_Sub_Set.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | /*
8 | * arr[] = {1, 5, 11, 5}
9 | Output: true
10 | The array can be partitioned as {1, 5, 5} and {11}
11 |
12 | arr[] = {1, 5, 3}
13 | Output: false
14 | The array cannot be partitioned into equal sum sets.
15 | */
16 | ///
17 | /// https://www.geeksforgeeks.org/partition-problem-dp-18/
18 | ///
19 | class Partition_problem_Sub_Set
20 | {
21 | // A utility function that returns true if there is a
22 | // subset of arr[] with sun equal to given sum
23 | static bool isSubsetSum(int[] arr, int n, int sum)
24 | {
25 | // Base Cases
26 | if (sum == 0)
27 | return true;
28 | if (n == 0 && sum != 0)
29 | return false;
30 |
31 | // If last element is greater than sum, then ignore it
32 | if (arr[n - 1] > sum)
33 | return isSubsetSum(arr, n - 1, sum);
34 |
35 | /* else, check if sum can be obtained by any of
36 | the following
37 | (a) including the last element
38 | (b) excluding the last element
39 | */
40 | return isSubsetSum(arr, n - 1, sum) ||
41 | isSubsetSum(arr, n - 1, sum - arr[n - 1]);
42 | }
43 |
44 | // Returns true if arr[] can be partitioned in two
45 | // subsets of equal sum, otherwise false
46 | static bool findPartition(int[] arr, int n)
47 | {
48 | // Calculate sum of the elements in array
49 | int sum = 0;
50 | for (int i = 0; i < n; i++)
51 | sum += arr[i];
52 |
53 | // If sum is odd, there cannot be two subsets
54 | // with equal sum
55 | if (sum % 2 != 0)
56 | return false;
57 |
58 | // Find if there is subset with sum equal to half
59 | // of total sum
60 | return isSubsetSum(arr, n, sum / 2);
61 | }
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/PowerOf.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.Practice
8 | {
9 | public class PowerOf
10 | {
11 | public static int PowerOf2(int n)
12 | {
13 | if(n < 1)
14 | {
15 | return 0;
16 | }
17 | else if (n == 1)
18 | {
19 | return 1;
20 | }
21 | else
22 | {
23 | int prev = PowerOf2(n / 2);
24 | int curr = prev * 2;
25 | Console.WriteLine(curr);
26 | return curr;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Power_Set.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | ///
8 | /// https://www.geeksforgeeks.org/power-set/
9 | /// Power Set Power set P(S) of a set S is the set of all subsets of S.
10 | /// For example S = {a, b, c} then P(s) = {{}, {a}, {b}, {c}, {a,b}, {a, c}, {b, c}, {a, b, c}}.
11 | ///If S has n elements in it then P(s) will have 2^n elements
12 | ///
13 | class Power_Set
14 | {
15 | static void printPowerSet(char[] set,
16 | int set_size)
17 | {
18 | /*set_size of power set of a set
19 | with set_size n is (2**n -1)*/
20 | uint pow_set_size =
21 | (uint)Math.Pow(2, set_size);
22 | int counter, j;
23 |
24 | /*Run from counter 000..0 to
25 | 111..1*/
26 | for (counter = 0; counter <
27 | pow_set_size; counter++)
28 | {
29 | for (j = 0; j < set_size; j++)
30 | {
31 | /* Check if jth bit in the
32 | counter is set If set then
33 | print jth element from set */
34 | if ((counter & (1 << j)) > 0)
35 | Console.Write(set[j]);
36 | }
37 |
38 | Console.WriteLine();
39 | }
40 | }
41 |
42 | }
43 | }
44 |
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Search_Word_2D_Grid.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | //class Search_Word_2D_Grid
8 | //{
9 | // // Rows and columns in given grid
10 | // static int R, C;
11 |
12 | // // For searching in all 8 direction
13 | // static int[] x = { -1, -1, -1, 0, 0, 1, 1, 1 };
14 | // static int[] y = { -1, 0, 1, -1, 1, -1, 0, 1 };
15 |
16 | // // This function searches in all 8-direction from point
17 | // // (row, col) in grid[][]
18 | // static bool search2D(char[][] grid, int row,
19 | // int col, String word)
20 | // {
21 | // // If first character of word doesn't match with
22 | // // given starting point in grid.
23 | // if (grid[row][col] != word[0])
24 | // return false;
25 |
26 | // int len = word.Length;
27 |
28 | // // Search word in all 8 directions
29 | // // starting from (row,col)
30 | // for (int dir = 0; dir < 8; dir++)
31 | // {
32 | // // Initialize starting point
33 | // // for current direction
34 | // int k, rd = row + x[dir], cd = col + y[dir];
35 |
36 | // // First character is already checked,
37 | // // match remaining characters
38 | // for (k = 1; k < len; k++)
39 | // {
40 | // // If out of bound break
41 | // if (rd >= R || rd < 0 || cd >= C || cd < 0)
42 | // break;
43 |
44 | // // If not matched, break
45 | // if (grid[rd][cd] != word.charAt(k))
46 | // break;
47 |
48 | // // Moving in particular direction
49 | // rd += x[dir];
50 | // cd += y[dir];
51 | // }
52 |
53 | // // If all character matched, then value of must
54 | // // be equal to length of word
55 | // if (k == len)
56 | // return true;
57 | // }
58 | // return false;
59 | // }
60 |
61 | // // Searches given word in a given
62 | // // matrix in all 8 directions
63 | // static void patternSearch(char[][] grid, String word)
64 | // {
65 | // // Consider every point as starting
66 | // // point and search given word
67 | // for (int row = 0; row < R; row++)
68 | // {
69 | // for (int col = 0; col < C; col++)
70 | // {
71 | // if (search2D(grid, row, col, word))
72 | // //System.out.println("pattern found at " + row + ", " + col);
73 |
74 | // }
75 | // }
76 | // }
77 | //}
78 |
79 | }
80 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/Stair_Case.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | public class Stair_Case
8 | {
9 | public static void Steps()
10 | {
11 | int[] X = { 1, 2, 3 };
12 | int result = Steps(4, X);
13 | int result1 = Steps_Mem(4, X);
14 | }
15 | #region Stair Case problem , very slow
16 | ///
17 | /// https://dailycodingproblem.com/blog/staircase-problem/
18 | /// https://www.techiedelight.com/find-total-ways-reach-nth-stair-with-atmost-m-steps/
19 | /// very slow (O(|X|^N)) since we are repeating computations again.
20 | /// We can use dynamic programming to speed it up.
21 | ///
22 | ///
23 | ///
24 | ///
25 | private static int Steps(int n, int[] X)
26 | {
27 | if (n < 0)
28 | {
29 | return 0;
30 | }
31 | else if (n == 0) return 1;
32 | else
33 | {
34 | int count = 0;
35 | for(int i = 0; i < X.Length; i++)
36 | {
37 | count += Steps(n - X[i], X);
38 | }
39 | return count;
40 | }
41 | }
42 | #endregion
43 |
44 | #region Memerozation
45 | static int[] mem = new int[100];
46 | private static int Steps_Mem(int n,int[] X)
47 | {
48 | int[] arr = new int[n + 1];
49 | arr[0] = 1;
50 | arr[1] = 1;
51 | arr[2] = 2;
52 |
53 | for (int i = 3; i < n + 1; i++)
54 | {
55 | arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3];
56 | }
57 | return arr[n];
58 | }
59 | #endregion
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/largest_sum_of_non_adjacent_numbers.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.GeeksForGeeks
8 | {
9 | public class largest_sum_of_non_adjacent_numbers
10 | {
11 | ///
12 | /// https://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/
13 | ///
14 | public static void sum_of_adjacent()
15 | {
16 | int[] arr = { 2, 4, 6, 2, 5 }; //13
17 | sum_of_adjacent(arr);
18 | }
19 |
20 | private static int sum_of_adjacent(int[] arr)
21 | {
22 | int incl = arr[0];
23 | int excl = 0;
24 | int temp = 0;
25 | for (int i = 1; i < arr.Length; i++)
26 | {
27 | temp = incl;
28 | incl = Math.Max(excl + arr[i], incl);
29 | excl = temp;
30 | }
31 |
32 | return Math.Max(incl, excl);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Coding_Exercises/print_matrix_spiral_form.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Coding_Exercises
6 | {
7 | class print_matrix_spiral_form
8 | {
9 | // Function print matrix in spiral form
10 | static void spiralPrint(int m, int n, int[,] a)
11 | {
12 | int i, k = 0, l = 0;
13 | /* k - starting row index
14 | m - ending row index
15 | l - starting column index
16 | n - ending column index
17 | i - iterator
18 | */
19 |
20 | while (k < m && l < n)
21 | {
22 | // Print the first row from the remaining rows
23 | for (i = l; i < n; ++i)
24 | {
25 | Console.Write(a[k, i] + " ");
26 | }
27 | k++;
28 |
29 | // Print the last column from the remaining columns
30 | for (i = k; i < m; ++i)
31 | {
32 | Console.Write(a[i, n - 1] + " ");
33 | }
34 | n--;
35 |
36 | // Print the last row from the remaining rows */
37 | if (k < m)
38 | {
39 | for (i = n - 1; i >= l; --i)
40 | {
41 | Console.Write(a[m - 1, i] + " ");
42 | }
43 | m--;
44 | }
45 |
46 | // Print the first column from the remaining columns */
47 | if (l < n)
48 | {
49 | for (i = m - 1; i >= k; --i)
50 | {
51 | Console.Write(a[i, l] + " ");
52 | }
53 | l++;
54 | }
55 | }
56 | }
57 |
58 | // Driver program
59 | public static void Main_2()
60 | {
61 | int R = 3;
62 | int C = 6;
63 | int[,] a = { {1, 2, 3, 4, 5, 6},
64 | {7, 8, 9, 10, 11, 12},
65 | {13, 14, 15, 16, 17, 18}
66 | };
67 | spiralPrint(R, C, a);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DBMS/README.md:
--------------------------------------------------------------------------------
1 | ### DBMS
2 | A database management system (DBMS) is system software for creating and managing databases. The DBMS provides users and programmers with a systematic way to create, retrieve, update and manage data.
3 | There are many types of database but will consider SQL and NOSQL database.
4 |
5 | #### Difference between SQL and NOSQL
6 |
7 | ![alt text][sqlnosql]
8 |
9 | [sqlnosql]: https://github.com/senthil338/coding_interview/blob/master/Images/SQLNOSQL.png "SQL vs NOSQL"
10 |
11 | #### Below are some of the features of NoSQL:
12 | **1.Flexibility:** NoSQL offers flexibility to store structured, semi-structured or unstructured data, unlike a relational database which allows only structured data.
13 |
14 | **2. Dynamic Schemas:** In NoSQL schema definition is not required, this solves the problem to modify the schema where a table is already present with huge datasets and new columns need to be added to the same table.
15 |
16 | **3.Sharding:** Sharding means partitioning data into smaller databases to have faster access to data. This feature is present in NoSQL databases which allow us to fetch data from a server in fastest time.
17 |
18 | **4.Generic:** NoSQL database can be customized by the user as per the need.
19 |
20 | **5.Scaling:** NoSQL databases scale out horizontally thus, they are cheaper to manage.
21 |
22 | #### What is the CAP theorem
23 |
24 | **1.Consistency:** Data in the database must be consistent before and after the execution of any operation. For example, after an update operation, every user should see the same data.
25 |
26 | **2.Availability:** System should always be up and running, there should be no downtime.
27 |
28 | **3.Partition Tolerance:** System should work even if the communication among the servers is unreliable.
29 | Theoretically, it is not possible to achieve all three requirements. CAP theorem allows distributed systems to follow any two of these requirements. Since partition tolerance is mandatory for distributed databases. Thus, we are left only with CP (Consistency, Partition tolerance) and AP (availability, Partition tolerance).
30 | Some of the examples of AP systems are **Dynamo, Cassandra, Simple DB, CouchDB**
31 | Some of the examples of CP systems are **Big Table, Hyper Table, Mongo DB, HBase**
32 |
33 | #### What is BASE in NoSQL
34 | The CAP theorem states that distributed systems cannot achieve all three properties at the same time; consistency, availability and partition tolerance. The BASE system gives up on consistency while maintaining the other two. The BASE system works well despite physical network partitions and always allow a client with reading and write availability.
35 | BASE stands for:
36 | **B**asically **A**vailable
37 | **S**oft state
38 | **E**ventual consistency
39 |
40 | #### What are the different types of NoSQL databases
41 | 1.Document Database
42 | 2.Column Database
43 | 3. Key-Value stores
44 | 4. Cache systems: These are similar to key-value pairs. They store frequently accessed data in memory for fast retrieval and access.
45 | 5. Graph database
46 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Array/README.md:
--------------------------------------------------------------------------------
1 | #### Some important topic to be considered
2 | 1. Write a program to find duplicate elements in an array
3 | 2. Write a program to find continuous sub array whose sum is equal to a given number
4 | 3. Write a program to find a missing number in an integer array
5 | 4. Write a program to count occurrences of each element in an array
6 | 5. Write a program to reverse an array without using an additional array
7 |
8 | ### Array
9 |
10 | An array, is a data structure consisting of a collection of elements (values or variables), each identified by at least one array index.
11 |
12 |
13 |
14 | 1. An array stores a fixed-size sequential collection of elements of the same type.
15 | - (i.e) char array only accept the char likewise integer only accept int value
16 | 2. Array is a reference type, so you need to use the new keyword to create an instance of the array. For example,
17 | - int[] balance = new int[10];
18 | 3. You can assign values to the array at the time of declaration, as shown,
19 | - char[] arr1 = {'g', 'e', 'e', 'k', 's'};
20 | - You can also create and initialize an array, as shown
21 | - int [] marks = new int[5] { 99, 98, 92, 97, 95};
22 |
23 |
24 |
25 |
26 | #### Types of Array
27 | - **One dimensional arrays**
28 | char[] arr1 = {'g', 'e', 'e', 'k', 's'};
29 | - **Two dimensional**
30 | int[,] array = new int[4, 2];
31 | - **Multi dimensional arrays**
32 | int[, ,] array1 = new int[4, 2, 3];
33 | - **Jagged arrays** - A jagged array is an array whose elements are arrays. The elements of a jagged array can be of different dimensions and sizes. A jagged array is sometimes called an "array of arrays."
34 |
35 | int[][] jaggedArray = new int[3][];
36 | jaggedArray[0] = new int[5];
37 | jaggedArray[1] = new int[4];
38 | jaggedArray[2] = new int[2];
39 |
40 | Reference : [Multidimensional-arrays](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/multidimensional-arrays)
41 |
Reference : [Jagged-arrays](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/jagged-arrays)
42 |
43 |
44 | ##### Basic Operations on Arrays and Complexity
45 | - Insert - O(n), Get - O(1), Delete - O(n) , Size
46 |
47 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/BinarySearchTree/BinarySearch.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.BinarySearchTree
6 | {
7 | public class BinarySearch
8 | {
9 |
10 | //public static int binarySearch(int[] array,int search)
11 | public static int binarySearch()
12 | {
13 | int[] array = { 1, 2, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 25, 26, 34, 35, 45, 56, 67, 78 };
14 | int first, last, middle, search;
15 | search = 26;
16 | first = 0;
17 | last = array.Length - 1;
18 | middle = (first + last) / 2;
19 |
20 | while (first <= last)
21 | {
22 | if (array[middle] < search)
23 | first = middle + 1;
24 | else if (array[middle] == search)
25 | {
26 | Console.WriteLine("{0} found at location {1}.\n", search, middle + 1);
27 | break;
28 | }
29 | else
30 | last = middle - 1;
31 |
32 | middle = (first + last) / 2;
33 | }
34 | if (first > last)
35 | Console.WriteLine("Not found! %d is not present in the list.\n", search);
36 |
37 | return 0;
38 | }
39 |
40 | ///
41 | /// Count the number of occurrences in a sorted array
42 | /// Input: arr[] = {1, 1, 2, 2, 2, 2, 3,}, x = 2
43 | /// Output: 4 // x (or 2) occurs 4 times in arr[]
44 | ///
45 | ///
46 | public static int binarySearchCount()
47 | {
48 | int[] array = { 1, 2, 2, 2, 3, 4, 5, 7, 8, 9, 11, 12, 13, 15, 25, 26, 34, 35, 45, 56, 67, 78 };
49 | int first, last, middle, search;
50 | search = 2;
51 | first = 0;
52 | last = array.Length - 1;
53 | middle = (first + last) / 2;
54 | int count = 0;
55 | while (first <= last)
56 | {
57 | if (array[middle] < search)
58 | first = middle + 1;
59 | else if (array[middle] == search)
60 | {
61 | count++;
62 | first = middle + 1;
63 | // Console.WriteLine("{0} found at location {1}.\n", search, middle + 1);
64 | //break;
65 | }
66 | else
67 | last = middle - 1;
68 |
69 | middle = (first + last) / 2;
70 | }
71 | if (first > last)
72 | Console.WriteLine("Not found! %d is not present in the list.\n", search);
73 |
74 | return count;
75 | }
76 |
77 |
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/DFS/BFS_1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.DFS
6 | {
7 | class BFS_1
8 | {
9 | static void Main_2(string[] args)
10 | {
11 | Node topNode = new Node() { Value = 1 };
12 | topNode.Neighbours.Add(new Node()
13 | {
14 | Value = 2,
15 | Neighbours = new List { new Node() { Value = 5,
16 | Neighbours = new List< Node > { new Node() { Value = 9 } } }
17 | }
18 | });
19 | topNode.Neighbours.Add(new Node()
20 | {
21 | Value = 3,
22 | Neighbours = new List { new Node() { Value = 6,
23 | Neighbours = new List() { new Node() { Value = 10 } } }, new Node() { Value = 7 } }
24 | });
25 | topNode.Neighbours.Add(new Node() { Value = 4,
26 | Neighbours = new List() { new Node() { Value = 8 } } });
27 |
28 |
29 | Queue queue = new Queue();
30 | queue.Enqueue(topNode);
31 |
32 | while (queue.Count > 0)
33 | {
34 | var element = queue.Dequeue();
35 | if (element.Neighbours.Count > 0)
36 | {
37 | Console.WriteLine(element.Value);
38 | foreach (var item in element.Neighbours)
39 | {
40 | queue.Enqueue(item);
41 | }
42 | }
43 | else
44 | Console.WriteLine(element.Value);
45 | }
46 |
47 | Console.Read();
48 | }
49 |
50 | public class Node
51 | {
52 | public Node()
53 | {
54 | Neighbours = new List();
55 | }
56 |
57 | public int Value { get; set; }
58 | public IList Neighbours { get; set; }
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/DFS/DFS_1.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure
6 | {
7 |
8 | public class DFS_1
9 | {
10 | public class Vertex
11 | {
12 | public string Letter;
13 | public bool Visited;
14 | public Vertex(string Letter)
15 | {
16 | this.Letter = Letter;
17 | this.Visited = false;
18 | }
19 | }
20 | public class Graph
21 | {
22 | private static int Max_Vertex = 20;
23 | private static Vertex[] Vertex_List;
24 | private static int[][] adjMat;
25 | private static int nVerts;
26 | private static Stack s;
27 |
28 | public Graph()
29 | {
30 | Vertex_List = new Vertex[Max_Vertex];
31 | adjMat = new int[Max_Vertex][];
32 | nVerts = 0;
33 | s = new Stack();
34 | for(int i =0;i< Max_Vertex;i++)
35 | {
36 | adjMat[i] = new int[Max_Vertex];
37 | }
38 | }
39 |
40 | public void AddVertex(string letter)
41 | {
42 | Vertex_List[nVerts++] = new Vertex(letter);
43 | }
44 |
45 | public void AddEdge(int start,int end)
46 | {
47 | adjMat[start][end] = 1;
48 | adjMat[end][start] = 1;
49 |
50 | }
51 |
52 | public static void dfs()
53 | {
54 | Add();
55 | Vertex_List[0].Visited = true;
56 | printVertex(0);
57 | s.Push(0);
58 | while (s.Count != 0)
59 | {
60 |
61 | int v = GetAdjUnvisitedVetex(s.Peek());
62 | if (v == -1)
63 | {
64 | s.Pop();
65 | }
66 | else
67 | {
68 | Vertex_List[v].Visited = true;
69 | printVertex(v);
70 | s.Push(v);
71 | }
72 | }
73 | }
74 | public static int GetAdjUnvisitedVetex(int v)
75 | {
76 | for(int j = 0; j < nVerts; j++)
77 | {
78 | if(adjMat[v][j] == 1 && Vertex_List[j].Visited == false)
79 | {
80 | return j;
81 | }
82 | }
83 | return -1;
84 | }
85 | public static void Add()
86 | {
87 | Graph graph = new Graph();
88 | graph.AddVertex("A");
89 | graph.AddVertex("B");
90 | graph.AddVertex("C");
91 | graph.AddVertex("D");
92 | graph.AddVertex("E");
93 | graph.AddVertex("F");
94 | graph.AddVertex("G");
95 | graph.AddVertex("H");
96 |
97 | graph.AddEdge(0, 1);
98 | graph.AddEdge(0, 3);
99 | graph.AddEdge(0, 6);
100 | graph.AddEdge(1, 4);
101 | graph.AddEdge(1, 5);
102 | graph.AddEdge(2, 5);
103 | graph.AddEdge(2, 7);
104 | graph.AddEdge(3, 5);
105 | graph.AddEdge(4, 6);
106 | }
107 |
108 | public static void printVertex(int v)
109 | {
110 | Console.WriteLine(Vertex_List[v].Letter + " ");
111 | }
112 | }
113 | }
114 | }
115 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/DoublyLinkedList/DoublyLinkedList.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.DoublyLinkedList
6 | {
7 | public class DoublyLinkedList
8 | {
9 | public class Node
10 | {
11 | int Data;
12 | public Node Prev;
13 | public Node Next;
14 | public Node(int data)
15 | {
16 | this.Data = data;
17 | Prev = Next = null;
18 | }
19 | }
20 |
21 | Node head;
22 | public void Insert(int data)
23 | {
24 | Node newNode = new Node(data);
25 | if (head == null)
26 | {
27 | head = newNode;
28 | }
29 | Node current = head;
30 | while (current.Next != null)
31 | {
32 | current = current.Next;
33 | }
34 |
35 | current.Next = newNode;
36 | newNode.Prev = current;
37 | }
38 |
39 | public void InsertAtFront(int data)
40 | {
41 | Node newNode = new Node(data);
42 | newNode.Next = head;
43 | head.Prev = newNode;
44 |
45 | }
46 |
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/DoublyLinkedList/README.md:
--------------------------------------------------------------------------------
1 | Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the
2 | sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) ,
3 | pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure.
4 |
5 | ![alt text][img]
6 |
7 | [img]: https://github.com/senthil338/coding_interview/blob/master/Images/doubly-linked-list2.png "Logo Title Text 2"
8 |
9 | ```cs
10 | class Node
11 | {
12 | Node prev;
13 | int data;
14 | Node next;
15 | }
16 | ```
17 |
18 | #### Advantages over singly linked list
19 | 1) A DLL can be traversed in both forward and backward direction.
20 | 2) The delete operation in DLL is more efficient if pointer to the node to be deleted is given.
21 |
22 |
23 | #### Disadvantages over singly linked list
24 |
25 | 1) Every node of DLL Require extra space for an previous pointer. It is possible to implement DLL with single pointer though (See this and this).
26 | 2) All operations require an extra pointer previous to be maintained. For example, in insertion, we need to modify previous pointers together with next pointers. For example in following functions for insertions at different positions, we need 1 or 2 extra steps to set previous pointer.
27 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/HashTable/HashTableImp.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.HashTable
6 | {
7 | ///
8 | /// Simple hashtable implementation with constant array value,
9 | /// Key collison not handled as it just for the reference and how Add and FindByKey works
10 | /// Time Complexity : Avg Search, Insert , Delete :- O(1) and Worst O(n) when Key collison occure's
11 | ///
12 | public class HashtableImp
13 | {
14 | private struct bucket
15 | {
16 | public object Key;
17 | public object Value;
18 | private int has_coll;
19 | }
20 | private const int MAX_SIZE = 20;
21 | private bucket[] buckets = new bucket[MAX_SIZE];
22 |
23 | public void Add(object key, object value)
24 | {
25 |
26 | int count = 0;
27 | while (buckets.Length > count)
28 | {
29 | if (buckets[count].Key == key)
30 | {
31 | throw new Exception("Key already present.");
32 | }
33 | if (buckets[count].Key == null)
34 | {
35 | buckets[count].Value = value;
36 | buckets[count].Key = key;
37 | return;
38 | }
39 | count++;
40 | }
41 | }
42 |
43 | public object FindByKey(object key)
44 | {
45 | foreach (bucket _bucket in buckets)
46 | {
47 | if (_bucket.Key == key)
48 | {
49 | return _bucket;
50 | }
51 | }
52 | return null;
53 | }
54 |
55 | public object this[int i]
56 | {
57 | get
58 | {
59 | return buckets[i];
60 | }
61 | }
62 |
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/HashTable/README.MD:
--------------------------------------------------------------------------------
1 | [Source Code](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/HashTable/HashTableImp.cs)
2 |
3 | #### Important Topic
4 |
5 | 1. Given an unsorted arrays, find all pairs whose sum is x
6 | 2. key-value database (DynamoDB)
7 | 3. Bloom Filter
8 |
9 | # HashTable
10 |
11 | A hash table is a data structure that is used to store keys/value pairs. Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects
12 | which maps keys to values. Any non-null object can be used as a key or as a value.
13 |
14 | ### Basic Opearation
15 |
16 | | Operation | Average | Worst |
17 | | --- | --- | --- |
18 | | Space | O(n) | O(n) |
19 | | Search | O(1) | O(n) |
20 | | Insert | O(1) | O(n) |
21 | | Delete | O(1) | O(n) |
22 |
23 | Hash key collision :- A collision will occur when two different keys have the same hashCode.
24 |
25 | #### Disadvantage
26 | The look up is not always O(1) in Worst case its O(n) when key collision occures
27 |
28 | [Wiki](https://en.wikipedia.org/wiki/Hash_table)
29 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Heap/MaxHeap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.DataStructure
8 | {
9 | public class MaxHeap
10 | {
11 | private readonly int[] _elements;
12 | private int _size;
13 |
14 | public MaxHeap(int size)
15 | {
16 | _elements = new int[size];
17 | }
18 |
19 | private int GetLeftChildIndex(int elementIndex) => 2 * elementIndex + 1;
20 | private int GetRightChildIndex(int elementIndex) => 2 * elementIndex + 2;
21 | private int GetParentIndex(int elementIndex) => (elementIndex - 1) / 2;
22 |
23 | private bool HasLeftChild(int elementIndex) => GetLeftChildIndex(elementIndex) < _size;
24 | private bool HasRightChild(int elementIndex) => GetRightChildIndex(elementIndex) < _size;
25 | private bool IsRoot(int elementIndex) => elementIndex == 0;
26 |
27 | private int GetLeftChild(int elementIndex) => _elements[GetLeftChildIndex(elementIndex)];
28 | private int GetRightChild(int elementIndex) => _elements[GetRightChildIndex(elementIndex)];
29 | private int GetParent(int elementIndex) => _elements[GetParentIndex(elementIndex)];
30 |
31 | private void Swap(int firstIndex, int secondIndex)
32 | {
33 | var temp = _elements[firstIndex];
34 | _elements[firstIndex] = _elements[secondIndex];
35 | _elements[secondIndex] = temp;
36 | }
37 |
38 | public bool IsEmpty()
39 | {
40 | return _size == 0;
41 | }
42 |
43 | public int Peek()
44 | {
45 | if (_size == 0)
46 | throw new IndexOutOfRangeException();
47 |
48 | return _elements[0];
49 | }
50 |
51 | public int Pop()
52 | {
53 | if (_size == 0)
54 | throw new IndexOutOfRangeException();
55 |
56 | var result = _elements[0];
57 | _elements[0] = _elements[_size - 1];
58 | _size--;
59 |
60 | ReCalculateDown();
61 |
62 | return result;
63 | }
64 |
65 | public void Add(int element)
66 | {
67 | if (_size == _elements.Length)
68 | throw new IndexOutOfRangeException();
69 |
70 | _elements[_size] = element;
71 | _size++;
72 |
73 | ReCalculateUp();
74 | }
75 |
76 | private void ReCalculateDown()
77 | {
78 | int index = 0;
79 | while (HasLeftChild(index))
80 | {
81 | var biggerIndex = GetLeftChildIndex(index);
82 | if (HasRightChild(index) && GetRightChild(index) > GetLeftChild(index))
83 | {
84 | biggerIndex = GetRightChildIndex(index);
85 | }
86 |
87 | if (_elements[biggerIndex] < _elements[index])
88 | {
89 | break;
90 | }
91 |
92 | Swap(biggerIndex, index);
93 | index = biggerIndex;
94 | }
95 | }
96 |
97 |
98 | private void ReCalculateUp()
99 | {
100 | var index = _size - 1;
101 | while (!IsRoot(index) && _elements[index] > GetParent(index))
102 | {
103 | var parentIndex = GetParentIndex(index);
104 | Swap(parentIndex, index);
105 | index = parentIndex;
106 | }
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Heap/MinHeap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.DataStructure
8 | {
9 | public class MinHeap
10 | {
11 | private readonly int[] _elements;
12 | private int _size;
13 |
14 | public MinHeap(int size)
15 | {
16 | _elements = new int[size];
17 | }
18 |
19 | private int GetLeftChildIndex(int elementIndex) => 2 * elementIndex + 1;
20 | private int GetRightChildIndex(int elementIndex) => 2 * elementIndex + 2;
21 | private int GetParentIndex(int elementIndex) => (elementIndex - 1) / 2;
22 |
23 | private bool HasLeftChild(int elementIndex) => GetLeftChildIndex(elementIndex) < _size;
24 | private bool HasRightChild(int elementIndex) => GetRightChildIndex(elementIndex) < _size;
25 | private bool IsRoot(int elementIndex) => elementIndex == 0;
26 |
27 | private int GetLeftChild(int elementIndex) => _elements[GetLeftChildIndex(elementIndex)];
28 | private int GetRightChild(int elementIndex) => _elements[GetRightChildIndex(elementIndex)];
29 | private int GetParent(int elementIndex) => _elements[GetParentIndex(elementIndex)];
30 |
31 | private void Swap(int firstIndex, int secondIndex)
32 | {
33 | var temp = _elements[firstIndex];
34 | _elements[firstIndex] = _elements[secondIndex];
35 | _elements[secondIndex] = temp;
36 | }
37 |
38 | public bool IsEmpty()
39 | {
40 | return _size == 0;
41 | }
42 |
43 | public int Peek()
44 | {
45 | if (_size == 0)
46 | throw new IndexOutOfRangeException();
47 |
48 | return _elements[0];
49 | }
50 |
51 | public int Pop()
52 | {
53 | if (_size == 0)
54 | throw new IndexOutOfRangeException();
55 |
56 | var result = _elements[0];
57 | _elements[0] = _elements[_size - 1];
58 | _size--;
59 |
60 | ReCalculateDown();
61 |
62 | return result;
63 | }
64 |
65 | public void Add(int element)
66 | {
67 | if (_size == _elements.Length)
68 | throw new IndexOutOfRangeException();
69 |
70 | _elements[_size] = element;
71 | _size++;
72 |
73 | ReCalculateUp();
74 | }
75 |
76 | private void ReCalculateDown()
77 | {
78 | int index = 0;
79 | while (HasLeftChild(index))
80 | {
81 | var smallerIndex = GetLeftChildIndex(index);
82 | if (HasRightChild(index) && GetRightChild(index) < GetLeftChild(index))
83 | {
84 | smallerIndex = GetRightChildIndex(index);
85 | }
86 |
87 | if (_elements[smallerIndex] >= _elements[index])
88 | {
89 | break;
90 | }
91 |
92 | Swap(smallerIndex, index);
93 | index = smallerIndex;
94 | }
95 | }
96 |
97 | private void ReCalculateUp()
98 | {
99 | var index = _size - 1;
100 | while (!IsRoot(index) && _elements[index] < GetParent(index))
101 | {
102 | var parentIndex = GetParentIndex(index);
103 | Swap(parentIndex, index);
104 | index = parentIndex;
105 | }
106 | }
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Heap/README.MD:
--------------------------------------------------------------------------------
1 | Soure Code
2 | - [MinHeap](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Heap/MinHeap.cs)
3 | - [MaxHeap](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Heap/MaxHeap.cs)
4 |
5 | Important Topic.
6 | 1. Return max value from an array
7 | 2. Return min value from an array
8 |
9 | # Heap or Binary Heap
10 | A binary heap is a complete binary tree which satisfies the heap ordering property. The ordering can be one of two types:
11 | - The min-heap property: the value of each node is greater than or equal to the value of its parent, with the minimum-value element at the root.
12 | - The max-heap property: the value of each node is less than or equal to the value of its parent, with the maximum-value element at the root.
13 | A heap is useful data structure when you need to remove the object with the highest (or lowest) priority. A common use of a heap is to implement a priority queue
14 |
15 | #### Min Heap representaion
16 |
17 | ![alt text][minheap]
18 |
19 | [minheap]: https://github.com/senthil338/coding_interview/blob/master/Images/minheap.jpg "Min Heap"
20 |
21 | For any node in position k,
22 | its left child is located at 2*k index
23 | its right child is located at 2*k+1. index
24 | its parent is located at k/2 index
25 |
26 | #### Inserting into a min-heap
27 | - Place the new element in the next available position in the array.
28 | - Compare the new element with its parent. If the new element is smaller, than swap it with its parent.
29 | - Continue this process until either
30 | - the new element’s parent is smaller than or
31 | equal to the new element, or
32 | - the new element reaches the root (index 0 of
33 | the array)
34 |
35 |
36 | #### Removing from a heap
37 |
38 | - Place the root element in a variable to
39 | return later.
40 | - Remove the last element in the deepest
41 | level and move it to the root.
42 | - While the moved element has a value
43 | greater than at least one of its children,
44 | swap this value with the smaller-valued
45 | child.
46 | - Return the original root that was saved.
47 |
48 | #### Basic operation on heap and complexity
49 | - insert O(log n)
50 | - deleteMin O(log n)
51 | - remove O(log n)
52 | - findMin O(1)
53 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/LinkedList/Readme.md:
--------------------------------------------------------------------------------
1 | [Source Code](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/LinkedList/LinkedList.cs)
2 |
3 | #### Important Topic
4 |
5 | 1. Add a two linked list and return a new list with digits.
6 | 2. Insert a value at Kth position in a linked list
7 | 3. Reverse a linked list
8 | 4. Detect loop in a linked list
9 | 5. LRU (Least Recently Used) cache
10 |
11 | ### Linked List
12 | A linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration.
13 | A drawback of linked lists is that access time is linear. Faster access, such as random access, is not feasible. Arrays have better cache locality compared to linked lists.
14 |
15 |
17 |
18 | Reference :- https://en.wikipedia.org/wiki/Linked_list
19 |
20 | ##### Basic Operations on Linked List and Complexity
21 | - Insert - O(1), Get - O(n), Delete - O(1)
22 |
23 | Linked list require head node , reference pointer for next node , and data to store
24 |
25 | ```cs
26 | class Node {
27 | Node next;
28 | int val;
29 | Node(int val) {
30 | this.val = val;
31 | next = null;
32 | }
33 | }
34 | ```
35 | ### Types of Linked List
36 | Source code and more details can be found by clicking below link
37 | 1. [Singly Linked List](https://github.com/senthil338/coding_interview/tree/master/CodingPractice/CodingPractice/DataStructure/LinkedList)
38 |

40 | 2. [Doubly Linked List](https://github.com/senthil338/coding_interview/tree/master/CodingPractice/CodingPractice/DataStructure/DoublyLinkedList)
41 |
43 | 3. Circular Linked List
44 |
46 | - A circularly linked list, all nodes are linked in a continuous circle, without using null. Circularly linked lists can be either singly or doubly linked.
47 |
48 |
49 | #### Advantages
50 | 1. A linked list can be viewed as a group of items, each of which points to the item in its neighbourhood
51 | 2. A linked lists is dynamic data structure(no need to define initial size in). Therefore,the primary advantages of linked lists over arrays is that linked lists can grow or shrink in size during the execution of a programme.
52 | 3. Linear data structures such as stacks and queues can be implemented using a linked list.
53 |
54 |
55 | #### Disadvantages
56 | 1. They use more memory than arrays because of the storage used by their pointers.
57 | 2. Nodes in a linked list must be read in order from the beginning as linked lists are inherently sequential access.
58 | 3. Nodes are stored incontiguously, greatly increasing the time periods required to access individual elements within the list, especially with a CPU cache.
59 | 4. Difficulties arise in linked lists when it comes to reverse traversing. For instance, singly linked lists are cumbersome to navigate backwards[1] and while doubly linked lists are somewhat easier to read, memory is consumed in allocating space for a back-pointer.
60 |
61 | Reference : [wiki](https://en.wikipedia.org/wiki/Linked_list)
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Queue/QueueImp.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.Queue
6 | {
7 | ///
8 | /// First-In-First-Out:
9 | /// Queue Implementation using Linked List
10 | /// Enqueue :- Adds an object to the end of the Queue. Add after 4
11 | /// Dequeue :- Removes and returns the object at the beginning of the Queue. Remove 1
12 | /// Peek :- Returns the object at the beginning of the Queue without removing it. return 1
13 | /// Avg, Worst -> Search Time : - O(n), Insert :- O(1), Delete :- O(1)
14 | /// Space : O(n)
15 | ///
16 | public class QueueImp
17 | {
18 | public class Node
19 | {
20 | public Node Next;
21 | public object Key;
22 | public Node(object key)
23 | {
24 | this.Next = null;
25 | this.Key = key;
26 | }
27 | }
28 |
29 | public Node rear { get; set; } //private
30 | public Node front { get; set; } //private
31 |
32 | public int Count { get; set; }
33 | public void Enqueue(object key)
34 | {
35 | Node node = new Node(key);
36 | if (rear == null)
37 | {
38 | front = rear = node;
39 |
40 | }
41 | else
42 | {
43 | rear.Next = node;
44 | rear = node;
45 | }
46 | Count++;
47 | }
48 |
49 | public object Dequeue()
50 | {
51 | if (front == null)
52 | {
53 | throw new Exception("Queue is Empty");
54 | }
55 | object data = front.Key;
56 | front = front.Next;
57 | // If front becomes NULL, then change rear also as NULL
58 | if (this.front == null)
59 | this.rear = null;
60 |
61 | return data;
62 | }
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Queue/README.MD:
--------------------------------------------------------------------------------
1 | [Source Code](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Queue/QueueImp.cs)
2 |
3 | #### Important Topic
4 | 1. Breadth First Traversal
5 | 2. Level Order Tree Traversal
6 | 3. ZigZag Tree Traversal
7 |
8 | # Queue
9 |
10 | Queue stores the elements in FIFO style (First In First Out), exactly opposite of the Stack collection. It contains the elements in the order they were added.
11 |
12 | #### Basic Operation
13 | - Enqueue Adds an item into the queue O(1)
14 | - Dequeue Removes and returns an item from the beginning of the queue O(1)
15 | - Peek Returns an first item from the queue O(1)
16 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Stack/README.MD:
--------------------------------------------------------------------------------
1 | #### Source Code
2 | - Using [Array](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Stack/StackImp.cs)
3 | - Using [LinkedList](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Stack/Stack_LinkedList.cs)
4 |
5 | #### Important Topic
6 | 1. Depth-First Traversal
7 | 2. Determine brackets are balanced or not
8 | 3. Implement a max stack
9 |
10 |
11 | # Stack
12 |
13 | Stack is a linear data structure which follows a particular order in which the operations are performed
14 | Its represents a simple last-in-first-out (LIFO).
15 |
16 | #### Basic operation on Stack
17 |
18 | Search Time : - O(n), Push :- O(1), Pop :- O(1), Peek :- 0(1)
19 | Space : O(n)
20 |
21 | - Push, which adds an element to the collection, and
22 | - Pop, which removes the most recently added element that was not yet removed
23 | - Peek Returns the top item from the stack.
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Stack/StackImp.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.DataStructure.Stack
6 | {
7 |
8 | ///
9 | /// Stack implementaion using Array
10 | /// LIFO (last in, first out)
11 | /// Avg, Worst -> Search Time : - O(n), Insert :- O(1), Delete :- O(1)
12 | /// Space : O(n)
13 | ///
14 | public class StackImp
15 | {
16 | //Array to store values
17 | // Push -> to insert value
18 | //Pop -> to delete value
19 | //count to maintain Stack level (insert -> increase , Delete -> decrease
20 |
21 | public const int _arraySize = 10;
22 | private int _size = 0;
23 | private int _top;
24 | int[] store = new int[_arraySize];
25 |
26 | public void Push(int data)
27 | {
28 | if (_top < _arraySize)
29 | {
30 | store[_top] = data;
31 | _top++;
32 | }
33 | else
34 | {
35 | return;
36 | }
37 | }
38 |
39 | public int Pop()
40 | {
41 | int value = 0;
42 | if (_top > 0)
43 | {
44 | value = store[_top];
45 | _top--;
46 | }
47 | return value;
48 | }
49 |
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Stack/Stack_LinkedList.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.DataStructure
8 | {
9 | public class Stack_LinkedList
10 | {
11 |
12 | ///
13 | /// Linked List Node
14 | ///
15 | public class Node
16 | {
17 | public Node next;
18 | public T data;
19 | public Node(T data)
20 | {
21 | this.next = null;
22 | this.data = data;
23 | }
24 | }
25 | Node root;
26 | public void Push(T data)
27 | {
28 | if(root ==null)
29 | {
30 | root = new Node(data);
31 | return;
32 | }
33 | Node newNode = new Node(data);
34 | Node temp = root;
35 | newNode.next = temp;
36 | }
37 |
38 | public void Pop()
39 | {
40 | if(root == null)
41 | {
42 | return;
43 | }
44 | root = root.next;
45 | }
46 |
47 | public T Peek()
48 | {
49 | if (root == null)
50 | {
51 | Console.WriteLine("Stack is empty");
52 | return default(T);
53 | }
54 | else
55 | {
56 | return root.data;
57 | }
58 |
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Trees/BinaryTree.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace ConsoleApplication1.BinarySearch
8 | {
9 | public class BinaryTree
10 | {
11 | public class Node
12 | {
13 | public int data;
14 | public Node left;
15 | public Node right;
16 | public Node()
17 | {
18 | //this.data = data;
19 | left = right = null;
20 | }
21 | public Node(int data)
22 | {
23 | this.data = data;
24 | left = right = null;
25 | }
26 | }
27 | static Node tree;
28 | public static void Insert()
29 | {
30 | //recursive Insert
31 | int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
32 | Insert(tree, 0, arr.Length, arr);
33 |
34 | //using Queue
35 | for (int i = 0; i < arr.Length; i++)
36 | Insert(tree, arr[i]);
37 | }
38 |
39 | private static Node Insert(Node tree,int index,int size,int[] arr)
40 | {
41 |
42 | if(index < size)
43 | {
44 | tree.data = arr[index];
45 | Insert(tree.left = new Node(),2 * index + 1,size,arr);
46 | Insert(tree.right= new Node(), 2 * index + 2,size,arr);
47 | }
48 | return tree;
49 |
50 | }
51 |
52 | private static void Insert(Node node,int key)
53 | {
54 | Queue q = new Queue();
55 | q.Enqueue(node);
56 | while (q != null)
57 | {
58 | Node nd = q.Peek();
59 | q.Dequeue();
60 |
61 | if(nd.left == null)
62 | {
63 | nd.left = new Node(key);
64 | break;
65 | }
66 | else
67 | {
68 | q.Enqueue(nd.left);
69 | }
70 | if(nd.right == null)
71 | {
72 | nd.right = new Node(key);
73 | break;
74 | }
75 | else
76 | {
77 | q.Enqueue(nd.right);
78 | }
79 | }
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Trees/TreeNode.cs:
--------------------------------------------------------------------------------
1 |
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace TreeGraph
9 | {
10 | public class TreeNode
11 | {
12 | public string Value { get; set; }
13 | public List Neighbors { get; set; }
14 |
15 | public TreeNode(string s)
16 | {
17 | Value = s;
18 | Neighbors = new List();
19 | }
20 |
21 | public void GenerateTree()
22 | {
23 | TreeNode abe = new TreeNode("Abe");
24 | TreeNode homer = new TreeNode("Homer") ;
25 | TreeNode bart = new TreeNode("Bart");
26 | TreeNode lisa = new TreeNode("Lisa");
27 | TreeNode maggie = new TreeNode("Maggie");
28 |
29 | abe.Neighbors.Add(homer);
30 | homer.Neighbors.Add(bart);
31 | homer.Neighbors.Add(lisa);
32 | homer.Neighbors.Add(maggie);
33 | }
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Trees/readme1.md:
--------------------------------------------------------------------------------
1 | ttttttttt
2 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/Trie/README.MD:
--------------------------------------------------------------------------------
1 | [Source code](https://github.com/senthil338/coding_interview/blob/master/CodingPractice/CodingPractice/DataStructure/Trie/TrieNode.cs)
2 | #### Some important topic to be considered
3 |
4 | 1. Design Search Autocomplete System
5 | 2. Longest word in dictionary
6 | 3. Boggle Words or Find all possible words in a board of characters
7 |
8 |
9 |
10 | ### Tries
11 |
12 | Trie is an efficient information retrieval data structure. The term “trie” comes from the word retrieval, and is usually pronounced “try”, to distinguish it from other “tree” structures.
13 |
14 | ##### Basic Operations on Tries and Complexity
15 | - Insert - O(n), Get - O(n)
16 |
17 | Trie data strucure can be formed as shown for the given input [ peter, piper, picked, peck, pickled, peppers ].
18 |
19 | If we type pi, the auto complete system will return piper, picked and pickled, if we type pic then it will return picked and pickled. Example Google Search and Searching contact in the mobile.
20 |
21 |
22 |
24 |
25 |
26 |
27 | #### Trie can be implemented as below
28 |
29 |
30 |
31 | ```cs
32 | public class TrieNode
33 | {
34 | public TrieNode[] Child = new TrieNode[26];
35 |
36 | // isLeaf is true if the node represents
37 | // end of a word
38 | public bool leaf;
39 |
40 | // Constructor
41 | public TrieNode() {
42 | leaf = false;
43 | for (int i =0 ; i< SIZE ; i++)
44 | Child[i] = null;
45 | }
46 | }
47 | ```
48 |
49 | ##### References:
50 | 1. [wiki](https://en.wikipedia.org/wiki/Trie)
51 | 2. [medium](https://medium.com/basecs/trying-to-understand-tries-3ec6bede0014)
52 | 3. [geeksforgeeks](https://www.geeksforgeeks.org/trie-insert-and-search/)
53 | 4. [YouTube](https://www.youtube.com/watch?v=zIjfhVPRZCg)
54 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v2.0",
4 | "signature": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v2.0": {
9 | "CodingPractice/1.0.0": {
10 | "runtime": {
11 | "CodingPractice.dll": {}
12 | }
13 | }
14 | }
15 | },
16 | "libraries": {
17 | "CodingPractice/1.0.0": {
18 | "type": "project",
19 | "serviceable": false,
20 | "sha512": ""
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.dll
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.pdb
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\senth\\.dotnet\\store\\|arch|\\|tfm|",
5 | "C:\\Users\\senth\\.nuget\\packages",
6 | "C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
7 | ]
8 | }
9 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/DataStructure/bin/Debug/netcoreapp2.0/CodingPractice.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "tfm": "netcoreapp2.0",
4 | "framework": {
5 | "name": "Microsoft.NETCore.App",
6 | "version": "2.0.0"
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/AlienLanguage.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class AlienLanguage
8 | {
9 | public bool isAlienSorted(String[] words, String order)
10 | {
11 | for (int i = 0; i < words.Length - 1; i++)
12 | {
13 | if (!isAlienSorted(words[i], words[i + 1], order))
14 | {
15 | return false;
16 | }
17 | }
18 | return true;
19 |
20 | }
21 |
22 | private bool isAlienSorted(String word1, String word2, String order)
23 | {
24 | for (int i = 0, j = 0; i < word1.Length && j < word2.Length; i++, j++)
25 | {
26 | if (order.IndexOf(word1[i]) < order.IndexOf(word2[j]))
27 | {
28 | return true;
29 | }
30 | else if (order.IndexOf(word1[i]) > order.IndexOf(word2[j]))
31 | {
32 | return false;
33 | }
34 | }
35 |
36 | if (word1.Length > word2.Length)
37 | {
38 | return false;
39 | }
40 |
41 | return true;
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Anagram-Substring-Search.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Anagram_Substring_Search
8 | {
9 | public const int MAX = 256;
10 |
11 | // This function returns true if
12 | // contents of arr1[] and arr2[]
13 | // are same, otherwise false.
14 | public static bool compare(char[] arr1, char[] arr2)
15 | {
16 | for (int i = 0; i < MAX; i++)
17 | {
18 | if (arr1[i] != arr2[i])
19 | {
20 | return false;
21 | }
22 | }
23 | return true;
24 | }
25 | public static void search(string pat, string txt)
26 | {
27 | int M = pat.Length;
28 | int N = txt.Length;
29 | char[] countP = new char[MAX];
30 | char[] countTW = new char[MAX];
31 | for (int i = 0; i < M; i++)
32 | {
33 | (countP[pat[i]])++;
34 | (countTW[txt[i]])++;
35 | }
36 | for (int i = M; i < N; i++)
37 | {
38 | if (compare(countP, countTW))
39 | {
40 | Console.WriteLine("Found at Index " + (i - M));
41 | }
42 | // Add current character to
43 | // current window
44 | (countTW[txt[i]])++;
45 | // Remove the first character of
46 | // previous window
47 | countTW[txt[i - M]]--;
48 | }
49 |
50 | // Check for the last window in text
51 | if (compare(countP, countTW))
52 | {
53 | Console.WriteLine("Found at Index " +
54 | (N - M));
55 | }
56 | }
57 |
58 | // Driver Code
59 | public static void Main(string[] args)
60 | {
61 | string txt = "BACDGABCDA";
62 | string pat = "ABCD";
63 | search(pat, txt);
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/BadVersion.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class BadVersion
8 | {
9 |
10 | ///
11 | /// Time O(logN)
12 | /// space 1
13 | ///
14 | ///
15 | ///
16 | public int firstBadVersion(int n)
17 | {
18 | int left = 1;
19 | int right = n;
20 | while (left < right)
21 | {
22 | int mid = left + (right - left) / 2;
23 | if (isBadVersion(mid))
24 | {
25 | right = mid;
26 | }
27 | else
28 | {
29 | left = mid + 1;
30 | }
31 | }
32 | return left;
33 | }
34 |
35 | private bool isBadVersion(int n)
36 | {
37 | return false;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/BestTimeToBuyAndSellStock.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class BestTimeToBuyAndSellStock
8 | {
9 | ///
10 | /// Time O(n)
11 | /// space 1
12 | ///
13 | ///
14 | ///
15 | public int maxProfit(int[] prices)
16 | {
17 | int minprice = int.MaxValue;
18 | int maxprofit = 0;
19 | for (int i = 0; i < prices.Length; i++)
20 | {
21 | if (prices[i] < minprice)
22 | minprice = prices[i];
23 | else if (prices[i] - minprice > maxprofit)
24 | maxprofit = prices[i] - minprice;
25 | }
26 | return maxprofit;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Binary-Tree-Maximum-Path-Sum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Binary_Tree_Maximum_Path_Sum
8 | {
9 | int max_sum = int.MinValue;
10 |
11 | public int max_gain(TreeNode node)
12 | {
13 | if (node == null) return 0;
14 |
15 | // max sum on the left and right sub-trees of node
16 | int left_gain = Math.Max(max_gain(node.left), 0);
17 | int right_gain = Math.Max(max_gain(node.right), 0);
18 |
19 | // the price to start a new path where `node` is a highest node
20 | int price_newpath = node.val + left_gain + right_gain;
21 |
22 | // update max_sum if it's better to start a new path
23 | max_sum = Math.Max(max_sum, price_newpath);
24 |
25 | // for recursion :
26 | // return the max gain if continue the same path
27 | return node.val + Math.Max(left_gain, right_gain);
28 | }
29 |
30 | public int maxPathSum(TreeNode root)
31 | {
32 | max_gain(root);
33 | return max_sum;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/ClosesTwoPoint.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class ClosesTwoPoint
8 | {
9 | public int[][] kClosest(int[][] points, int K)
10 | {
11 | int N = points.Length;
12 | int[] dists = new int[N];
13 | for (int i = 0; i < N; ++i)
14 | dists[i] = dist(points[i]);
15 |
16 | Array.Sort(dists);
17 | int distK = dists[K - 1];
18 |
19 | int[][] ans = new int[K][];
20 | int t = 0;
21 | for (int i = 0; i < N; ++i)
22 | if (dist(points[i]) <= distK)
23 | ans[t++] = points[i];
24 | return ans;
25 | }
26 |
27 | public int dist(int[] point)
28 | {
29 | return point[0] * point[0] + point[1] * point[1];
30 | }
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/FindPeekElement.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class FindPeekElement
8 | {
9 | ///
10 | /// https://leetcode.com/explore/interview/card/facebook/54/sorting-and-searching-3/3032/
11 | /// run n(logn)
12 | /// space 1
13 | ///
14 | ///
15 | ///
16 | public int findPeakElement(int[] nums)
17 | {
18 | int l = 0, r = nums.Length - 1;
19 | while (l < r)
20 | {
21 | int mid = (l + r) / 2;
22 | if (nums[mid] > nums[mid + 1])
23 | r = mid;
24 | else
25 | l = mid + 1;
26 | }
27 | return l;
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/FlatternBST.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class FlatternBST
8 | {
9 | ///
10 | /// https://leetcode.com/explore/interview/card/facebook/52/trees-and-graphs/322/discuss/425617/Java-beat-100-Space-and-Time
11 | ///
12 | ///
13 | ///
14 | private TreeNode flattenInternal(TreeNode node)
15 | {
16 | if (node == null || (node.left == null && node.right == null)) return node;
17 |
18 | TreeNode leftRightMost = flattenInternal(node.left);
19 | TreeNode rightRightMost = flattenInternal(node.right);
20 |
21 | if (leftRightMost != null)
22 | {
23 | leftRightMost.right = node.right;
24 | node.right = node.left;
25 | node.left = null;
26 | }
27 |
28 | return rightRightMost == null ? leftRightMost : rightRightMost;
29 | }
30 |
31 | public void flatten(TreeNode root)
32 | {
33 | flattenInternal(root);
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Is_Heap.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | ///
8 | ///Arr[(i -1) / 2] returns its parent node.
9 | ///Arr[(2 * i) + 1] returns its left child node.
10 | ///Arr[(2 * i) + 2] returns its right child node.
11 | ///https://www.geeksforgeeks.org/how-to-check-if-a-given-array-represents-a-binary-heap/
12 | ///
13 | class Is_Heap
14 | {
15 | public static void isHeap()
16 | {
17 | int[] arr = { 90, 15, 10, 7, 12, 2, 7, 3 };
18 | isHeap(arr, 0, arr.Length-1);
19 | }
20 | #region recursive solution
21 | ///
22 | /// recursive
23 | ///
24 | ///
25 | ///
26 | ///
27 | ///
28 | static bool isHeap(int[] arr, int i, int n)
29 | {
30 | // If a leaf node
31 | if (i > (n - 2) / 2)
32 | {
33 | return true;
34 | }
35 |
36 | // If an internal node and is greater
37 | // than its children, and same is
38 | // recursively true for the children
39 | if (arr[i] >= arr[2 * i + 1] && arr[i] >= arr[2 * i + 2] &&
40 | isHeap(arr, 2 * i + 1, n) && isHeap(arr, 2 * i + 2, n))
41 | {
42 | return true;
43 | }
44 |
45 | return false;
46 | }
47 | #endregion
48 | #region iterative
49 | // Returns true if arr[i..n-1]
50 | // represents a max-heap
51 | static bool isHeap(int[] arr, int n)
52 | {
53 | // Start from root and go till
54 | // the last internal node
55 | for (int i = 0; i <= (n - 2) / 2; i++)
56 | {
57 | // If left child is greater,
58 | // return false
59 | if (arr[2 * i + 1] > arr[i])
60 | {
61 | return false;
62 | }
63 |
64 | // If right child is greater,
65 | // return false
66 | if (2 * i + 2 < n && arr[2 * i + 2] > arr[i])
67 | {
68 | return false;
69 | }
70 | }
71 | return true;
72 | }
73 | #endregion
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/LetterCombinationOfPhoneNumber.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class LetterCombinationOfPhoneNumber
8 | {
9 | Dictionary phone = new Dictionary();
10 | List output = new List();
11 | public LetterCombinationOfPhoneNumber()
12 | {
13 | phone.Add("2", "abc");
14 | phone.Add("3", "def");
15 | phone.Add("4", "ghi");
16 | phone.Add("5", "jkl");
17 | phone.Add("6", "mno");
18 | phone.Add("7", "pqrs");
19 | phone.Add("8", "tuv");
20 | phone.Add("9", "wxyz");
21 | }
22 |
23 | /*
24 | Time complexity : O(3^N * 4^M) where N is the number of digits in the input that maps to 3 letters (e.g. 2, 3, 4, 5, 6, 8)
25 | and M is the number of digits in the input that maps to 4 letters (e.g. 7, 9), and N+M is the total number digits in the input.
26 |
27 | Space complexity : \{O}(3^N * 4^M)
28 | since one has to keep 3^N * 4^M
29 | https://leetcode.com/explore/interview/card/facebook/53/recursion-3/267/
30 | */
31 | public void backtrack(String combination, String next_digits)
32 | {
33 | // if there is no more digits to check
34 | if (next_digits.Length == 0)
35 | {
36 | // the combination is done
37 | output.Add(combination);
38 | }
39 | // if there are still digits to check
40 | else
41 | {
42 | // iterate over all letters which map
43 | // the next available digit
44 | String digit = next_digits.Substring(0, 1);
45 | String letters = phone[digit];
46 | for (int i = 0; i < letters.Length; i++)
47 | {
48 | String letter = phone[digit].Substring(i, i + 1);
49 | // append the current letter to the combination
50 | // and proceed to the next digits
51 | backtrack(combination + letter, next_digits.Substring(1));
52 | }
53 | }
54 | }
55 |
56 | public List letterCombinations(String digits)
57 | {
58 | if (digits.Length != 0)
59 | backtrack("", digits);
60 | return output;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/MinJump.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class MinJump
8 | {
9 | ///
10 | /// https://github.com/mission-peace/interview/blob/master/src/com/interview/dynamic/MinJumpToReachEnd.java
11 | /// https://www.youtube.com/watch?v=cETfFsSTGJI
12 | /// * Space complexity O(n) to maintain result and min jumps
13 | ///* Time complexity O(n^2)
14 | ///
15 | ///
16 | ///
17 | ///
18 | public int minJump(int[] arr, int[] result)
19 | {
20 | int[] jump = new int[arr.Length];
21 | jump[0] = 0;
22 | for (int i = 1; i < arr.Length; i++)
23 | {
24 | jump[i] = int.MaxValue - 1;
25 | }
26 |
27 | for (int i = 1; i < arr.Length; i++)
28 | {
29 | for (int j = 0; j < i; j++)
30 | {
31 | if (arr[j] + j >= i)
32 | {
33 | if (jump[i] > jump[j] + 1)
34 | {
35 | result[i] = j;
36 | jump[i] = jump[j] + 1;
37 | }
38 | }
39 | }
40 | }
41 |
42 | return jump[jump.Length - 1];
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/MoveZero.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class MoveZero
8 | {
9 | ///
10 | /// https://leetcode.com/explore/interview/card/facebook/5/array-and-strings/262/
11 | ///
12 | ///
13 | void moveZeroes(int[] nums)
14 | {
15 | for (int lastNonZeroFoundAt = 0, cur = 0; cur < nums.Length; cur++)
16 | {
17 | if (nums[cur] != 0)
18 | {
19 | //swap(nums[lastNonZeroFoundAt++], nums[cur]);
20 | }
21 | }
22 | }
23 |
24 | void moveZeroes_1(int[] nums)
25 | {
26 | int lastNonZeroFoundAt = 0;
27 | // If the current element is not 0, then we need to
28 | // append it just in front of last non 0 element we found.
29 | for (int i = 0; i < nums.Length; i++)
30 | {
31 | if (nums[i] != 0)
32 | {
33 | nums[lastNonZeroFoundAt++] = nums[i];
34 | }
35 | }
36 | // After we have finished processing new elements,
37 | // all the non-zero elements are already at beginning of array.
38 | // We just need to fill remaining array with 0's.
39 | for (int i = lastNonZeroFoundAt; i < nums.Length; i++)
40 | {
41 | nums[i] = 0;
42 | }
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/OneEditDistance.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class OneEditDistance
8 | {
9 | static void Main_1()
10 | {
11 | string s = "ab", t = "acb";
12 | var result = isOneEditDistance(s, t);
13 | }
14 |
15 | /*
16 | There are 3 possiblities to satisify one edit distance apart:
17 | Insert a character into s to get t
18 | Delete a character from s to get t
19 | Replace a character of s to get t
20 | Input: s = "ab", t = "acb"
21 | Output: true
22 | Explanation: We can insert 'c' into s to get t.
23 |
24 | Input: s = "cab", t = "ad"
25 | Output: false
26 | Explanation: We cannot get t from s by only one step.
27 |
28 | Input: s = "1203", t = "1213"
29 | Output: true
30 | Explanation: We can replace '0' with '1' to get t.
31 | */
32 | public static bool isOneEditDistance(String s, String t)
33 | {
34 |
35 | int ns = s.Length;
36 | int nt = t.Length;
37 |
38 | // Ensure that s is shorter than t.
39 | if (ns > nt)
40 | return isOneEditDistance(t, s);
41 |
42 | // The strings are NOT one edit away distance
43 | // if the length diff is more than 1.
44 | if (nt - ns > 1)
45 | return false;
46 |
47 | for (int i = 0; i < ns; i++)
48 | if (s[i] != t[i])
49 | // if strings have the same length
50 | if (ns == nt)
51 | return s.Substring(i + 1).Equals(t.Substring(i + 1));
52 | // if strings have different lengths
53 | else
54 | return s.Substring(i).Equals(t.Substring(i + 1));
55 |
56 | // If there is no diffs on ns distance
57 | // the strings are one edit away only if
58 | // t has one more character.
59 | return (ns + 1 == nt);
60 | }
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Palindrom2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Palindrom2
8 | {
9 | class Solution
10 | {
11 | // time O(S.length+T.length) ,space O(T.Length)
12 | public String customSortString(String S, String T)
13 | {
14 | StringBuilder sb = new StringBuilder();
15 | int[] frequency = new int[26];
16 | foreach(char token in T.ToCharArray())
17 | {
18 | frequency[token - 'a']++;
19 | }
20 |
21 | foreach (char token in S.ToCharArray())
22 | {
23 | while (frequency[token - 'a'] != 0)
24 | {
25 | sb.Append(token);
26 | frequency[token - 'a']--;
27 | }
28 | }
29 |
30 | for (int i = 0; i < 26; i++)
31 | {
32 | while (frequency[i] > 0)
33 | {
34 | sb.Append((char)(i + 'a'));
35 | frequency[i]--;
36 | }
37 | }
38 | return sb.ToString();
39 | }
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Random-Pick-With-Weight.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Random_Pick_With_Weight
8 | {
9 | List psum = new List();
10 | int tot = 0;
11 | Random rand = new Random();
12 |
13 | public Random_Pick_With_Weight(int[] w)
14 | {
15 | foreach (int x in w)
16 | {
17 | tot += x;
18 | psum.Add(tot);
19 | }
20 | }
21 |
22 | public int pickIndex()
23 | {
24 | int targ = rand.Next(tot);
25 |
26 | int lo = 0;
27 | int hi = psum.Count - 1;
28 | while (lo != hi)
29 | {
30 | int mid = (lo + hi) / 2;
31 | if (targ >= psum[mid]) lo = mid + 1;
32 | else hi = mid;
33 | }
34 | return lo;
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/RemoveInvalidParentheses.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class RemoveInvalidParentheses
8 | {
9 | private HashSet validExpressions = new HashSet();
10 |
11 | private void recurse(String s, int index, int leftCount, int rightCount, int leftRem, int rightRem,
12 | StringBuilder expression)
13 | {
14 |
15 | // If we reached the end of the string, just check if the resulting expression is
16 | // valid or not and also if we have removed the total number of left and right
17 | // parentheses that we should have removed.
18 | if (index == s.Length)
19 | {
20 | if (leftRem == 0 && rightRem == 0)
21 | this.validExpressions.Add(expression.ToString());
22 | }
23 | else
24 | {
25 | char character = s[index];
26 | int length = expression.Length;
27 | // The discard case. Note that here we have our pruning condition.
28 | // We don't recurse if the remaining count for that parenthesis is == 0.
29 | if ((character == '(' && leftRem > 0) || (character == ')' && rightRem > 0))
30 | {
31 | this.recurse(s, index + 1,
32 | leftCount,
33 | rightCount,
34 | leftRem - (character == '(' ? 1 : 0),
35 | rightRem - (character == ')' ? 1 : 0),
36 | expression);
37 | }
38 |
39 | expression.Append(character);
40 |
41 | // Simply recurse one step further if the current character is not a parenthesis.
42 | if (character != '(' && character != ')')
43 | this.recurse(s, index + 1, leftCount, rightCount, leftRem, rightRem, expression);
44 | else if (character == '(')
45 | // Consider an opening bracket.
46 | this.recurse(s, index + 1, leftCount + 1, rightCount, leftRem, rightRem, expression);
47 | else if (rightCount < leftCount)
48 | // Consider a closing bracket.
49 | this.recurse(s, index + 1, leftCount, rightCount + 1, leftRem, rightRem, expression);
50 | // Delete for backtracking.
51 | expression.Clear().Append(expression.ToString().Remove(length));
52 | }
53 | }
54 |
55 | public List removeInvalidParentheses(String s)
56 | {
57 |
58 | int left = 0, right = 0;
59 | // First, we find out the number of misplaced left and right parentheses.
60 | for (int i = 0; i < s.Length; i++)
61 | {
62 | // Simply record the left one.
63 | if (s[i] == '(')
64 | left++;
65 | else if (s[i] == ')')
66 | {
67 | // If we don't have a matching left, then this is a misplaced right, record it.
68 | right = left == 0 ? right + 1 : right;
69 |
70 | // Decrement count of left parentheses because we have found a right
71 | // which CAN be a matching one for a left.
72 | left = left > 0 ? left - 1 : left;
73 | }
74 | }
75 | this.recurse(s, 0, 0, 0, left, right, new StringBuilder());
76 | return new List(this.validExpressions);
77 | }
78 | }
79 | }
80 |
81 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/SerializeAndDeSerializeBST.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using System.Linq;
5 |
6 | namespace CodingPractice.GeeksForGeeks
7 | {
8 | class SerializeAndDeSerializeBST
9 | {
10 | #region Serialize
11 | // Encodes a tree to a single string.
12 | public String serialize(TreeNode root)
13 | {
14 | return rserialize(root, "");
15 | }
16 |
17 | public String rserialize(TreeNode root, String str)
18 | {
19 | // Recursive serialization.
20 | if (root == null)
21 | {
22 | str += "null,";
23 | }
24 | else
25 | {
26 | str += root.val + ",";
27 | str = rserialize(root.left, str);
28 | str = rserialize(root.right, str);
29 | }
30 | return str;
31 | }
32 | #endregion
33 | #region Deserialize
34 | public TreeNode rdeserialize(List l)
35 | {
36 | // Recursive deserialization.
37 | if (l[0].Equals("null"))
38 | {
39 | l.RemoveAt(0);
40 | return null;
41 | }
42 |
43 | TreeNode root = new TreeNode(Convert.ToInt32(l[0]));
44 | l.RemoveAt(0);
45 | root.left = rdeserialize(l);
46 | root.right = rdeserialize(l);
47 | return root;
48 | }
49 | // Decodes your encoded data to tree.
50 | public TreeNode deserialize(String data)
51 | {
52 | String[] data_array = data.Split(",");
53 | List data_list = new List(data_array.OfType());
54 | return rdeserialize(data_list);
55 | }
56 | #endregion
57 |
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Subtree-of-another-tree.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Subtree_of_another_tree
8 | {
9 | /*
10 | Instead of creating an inorder traversal, we can treat every node of the given tree tt as the root, treat it as a subtree and compare the
11 | corresponding subtree with the given subtree ss for equality. For checking the equality, we can compare the all the nodes of the two subtrees.
12 | For doing this, we make use a function traverse(s,t) which traverses over the given tree ss and treats every node as the root of the
13 | subtree currently being considered. It also checks the two subtrees currently being considered for their equality.
14 | In order to check the equality of the two subtrees, we make use of equals(x,y) function, which takes xx and yy, which are the roots of
15 | the two subtrees to be compared as the inputs and returns True or False
16 | depending on whether the two are equal or not. It compares all the nodes of the two subtrees for equality.
17 | Time complexity : O(m∗n). In worst case(skewed tree) traverse function takes O(m*n)O(m∗n) time.
18 |
19 | Space complexity : O(n). The depth of the recursion tree can go upto nn. nn refers to the number of nodes in ss.
20 | */
21 | public bool isSubtree(TreeNode s, TreeNode t)
22 | {
23 | return traverse(s, t);
24 | }
25 | public bool equals(TreeNode x, TreeNode y)
26 | {
27 | if (x == null && y == null)
28 | return true;
29 | if (x == null || y == null)
30 | return false;
31 | return x.val == y.val && equals(x.left, y.left) && equals(x.right, y.right);
32 | }
33 | public bool traverse(TreeNode s, TreeNode t)
34 | {
35 | return s != null && (equals(s, t) || traverse(s.left, t) || traverse(s.right, t));
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/TreeNode.cs:
--------------------------------------------------------------------------------
1 | namespace CodingPractice.GeeksForGeeks
2 | {
3 | internal class TreeNode
4 | {
5 | public TreeNode left;
6 | public TreeNode right;
7 | public int val;
8 |
9 | public TreeNode(int val)
10 | {
11 |
12 | }
13 | }
14 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/UnionIntersection.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class UnionIntersection
8 | {
9 | #region unsorted array Union
10 | static void printUnion(int[] arr1, int[] arr2)
11 | {
12 | HashSet hs = new HashSet();
13 |
14 | for (int i = 0; i < arr1.Length; i++)
15 | hs.Add(arr1[i]);
16 | for (int i = 0; i < arr2.Length; i++)
17 | hs.Add(arr2[i]);
18 |
19 | Console.WriteLine(string.Join(", ", hs));
20 | }
21 | #endregion
22 |
23 | #region unsorted array Intersection
24 | // Prints intersection of arr1[0..m-1] and arr2[0..n-1]
25 | static void printIntersection(int[] arr1, int[] arr2)
26 | {
27 | HashSet hs = new HashSet();
28 |
29 | for (int i = 0; i < arr1.Length; i++)
30 | hs.Add(arr1[i]);
31 |
32 | for (int i = 0; i < arr2.Length; i++)
33 | if (hs.Contains(arr2[i]))
34 | Console.Write(arr2[i] + " ");
35 | }
36 | #endregion
37 |
38 | #region Union for Sorted Array
39 | static int printUnion(int[] arr1,
40 | int[] arr2, int m, int n)
41 | {
42 | int i = 0, j = 0;
43 |
44 | while (i < m && j < n)
45 | {
46 | if (arr1[i] < arr2[j])
47 | Console.Write(arr1[i++] + " ");
48 | else if (arr2[j] < arr1[i])
49 | Console.Write(arr2[j++] + " ");
50 | else
51 | {
52 | Console.Write(arr2[j++] + " ");
53 | i++;
54 | }
55 | }
56 |
57 | /* Print remaining elements of
58 | the larger array */
59 | while (i < m)
60 | Console.Write(arr1[i++] + " ");
61 | while (j < n)
62 | Console.Write(arr2[j++] + " ");
63 |
64 | return 0;
65 | }
66 | #endregion
67 |
68 | #region Intersction for Sorted Array
69 | static void printIntersection(int[] arr1,
70 | int[] arr2, int m, int n)
71 | {
72 | int i = 0, j = 0;
73 |
74 | while (i < m && j < n)
75 | {
76 | if (arr1[i] < arr2[j])
77 | i++;
78 | else if (arr2[j] < arr1[i])
79 | j++;
80 | else
81 | {
82 | Console.Write(arr2[j++] + " ");
83 | i++;
84 | }
85 | }
86 | }
87 | #endregion
88 | // Driver Code
89 | static void Main_1()
90 | {
91 | int[] arr1 = { 7, 1, 5, 2, 3, 6,8,20 };
92 | int[] arr2 = { 3, 8, 6, 20, 7 };
93 |
94 | Console.WriteLine("Union of two arrays is : ");
95 | printUnion(arr1, arr2);
96 |
97 | Console.WriteLine("\nIntersection of two arrays is : ");
98 | printIntersection(arr1, arr2);
99 | }
100 | }
101 | }
102 |
103 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/ValidPalindrome.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class ValidPalindrome
8 | {
9 | class Solution
10 | {
11 | ///
12 | /// https://leetcode.com/explore/interview/card/facebook/5/array-and-strings/289/
13 | ///
14 | ///
15 | ///
16 | ///
17 | ///
18 | public bool isPalindromeRange(String s, int i, int j)
19 | {
20 | for (int k = i; k <= i + (j - i) / 2; k++)
21 | {
22 | if (s[k] != s[j - k + i]) return false;
23 | }
24 | return true;
25 | }
26 | public bool validPalindrome(String s)
27 | {
28 | for (int i = 0; i < s.Length / 2; i++)
29 | {
30 | if (s[i] != s[s.Length - 1 - i])
31 | {
32 | int j = s.Length - 1 - i;
33 | return (isPalindromeRange(s, i + 1, j) ||
34 | isPalindromeRange(s, i, j - 1));
35 | }
36 | }
37 | return true;
38 | }
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/Work-Break-2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class Work_Break_2
8 | {
9 | /*
10 | Time complexity : O(n^3). Two loops are required to fill dp array and one loop for appending a list .
11 |
12 | Space complexity : O(n^3) Length of dp array is nn and each value of \text{dp}dp array contains a list of string i.e. n^2 space.
13 | */
14 | public List wordBreak(String s, HashSet wordDict)
15 | {
16 | List[] dp = new List[s.Length + 1];
17 | List initial = new List();
18 | initial.Add("");
19 | dp[0] = initial;
20 | for (int i = 1; i <= s.Length; i++)
21 | {
22 | List list = new List();
23 | for (int j = 0; j < i; j++)
24 | {
25 | if (dp[j].Count > 0 && wordDict.Contains(s.Substring(j, i)))
26 | {
27 | foreach (String l in dp[j])
28 | {
29 | list.Add(l + (l.Equals("") ? "" : " ") + s.Substring(j, i));
30 | }
31 | }
32 | }
33 | dp[i] = list;
34 | }
35 | return dp[s.Length];
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GeeksForGeeks/minimum-remove-to-make-valid-parentheses.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.GeeksForGeeks
6 | {
7 | class minimum_remove_to_make_valid_parentheses
8 | {
9 | public String minRemoveToMakeValid(String s)
10 | {
11 | Stack st = new Stack();
12 | StringBuilder sb = new StringBuilder();
13 | int i = 0;
14 | foreach(char c in s.ToCharArray())
15 | {
16 | if (c == '(' || c == ')')
17 | {
18 | if (st.Count==0)
19 | {
20 | st.Push(i);
21 | }
22 | else
23 | {
24 | if (c == ')' && s[st.Peek()] == '(')
25 | {
26 | st.Pop();
27 | }
28 | else
29 | {
30 | st.Push(i);
31 | }
32 | }
33 | }
34 | i++;
35 | }
36 | sb.Append(s);
37 | string str = sb.ToString();
38 | while (st.Count!=0)
39 | {
40 | int t = st.Pop();
41 | str = str.Remove(t);
42 | }
43 | return str;
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/GraphAlgorithm/Graph.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace CodingPractice.GraphAlgorithm
7 | {
8 | public class Graph
9 | {
10 | Dictionary nodeLookup = new Dictionary();
11 | public class Node
12 | {
13 | private int id;
14 | LinkedList adjacent = new LinkedList();
15 | Graph graph = new Graph();
16 | public Node(int id)
17 | {
18 | this.id = id;
19 |
20 | }
21 |
22 | private Node getNode(int id)
23 | {
24 |
25 | if (!graph.nodeLookup.ContainsKey(id))
26 | {
27 | Node node = new Node(id);
28 | graph.nodeLookup.Add(id, node);
29 | }
30 | return graph.nodeLookup.GetValueOrDefault(id);
31 |
32 | }
33 |
34 | public void AddEdge(int source,int destination)
35 | {
36 | Node s = getNode(source);
37 | Node d = getNode(destination);
38 | s.adjacent.AddLast(d);
39 | }
40 |
41 | public bool HasPathDFS(int source,int destination)
42 | {
43 | Node s = getNode(source);
44 | Node d = getNode(destination);
45 | HashSet visited = new HashSet();
46 | return hasPathDFS(s,d,visited);
47 | }
48 | #region Depth first search
49 | private bool hasPathDFS(Node source,Node destination, HashSet visited)
50 | {
51 | if (visited.Contains(source.id))
52 | {
53 | return true;
54 | }
55 | visited.Add(source.id);
56 | if(source == destination)
57 | {
58 | return true;
59 | }
60 | foreach(Node child in source.adjacent)
61 | {
62 | if (hasPathDFS(child, destination, visited))
63 | {
64 | return true;
65 | }
66 | }
67 | return false;
68 | }
69 | #endregion
70 | #region Breadh first search
71 | private bool hasPathBFS(Node source,Node destination)
72 | {
73 | LinkedList nextToVisit = new LinkedList();
74 | HashSet visited = new HashSet();
75 | nextToVisit.AddLast(source);
76 | while(nextToVisit.Count > 0)
77 | {
78 | Node node = nextToVisit.First.Value;
79 | nextToVisit.RemoveFirst();
80 | if (node == destination)
81 | {
82 | return true;
83 | }
84 | if (visited.Contains(node.id))
85 | {
86 | continue;
87 | }
88 | visited.Add(node.id);
89 | foreach(Node child in node.adjacent)
90 | {
91 | nextToVisit.AddLast(child);
92 | }
93 | }
94 | return false;
95 | }
96 | #endregion
97 | }
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/2DArray_DS.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class _2DArray_DS
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/2d-array/problem
11 | /// Easy 15
12 | ///
13 | ///
14 | ///
15 | static int hourglassSum(int[][] arr)
16 | {
17 | int sum = 0;
18 | int result = int.MinValue;
19 | for (int i = 0; i < arr.Length - 2; i++)
20 | {
21 | for (int j = 0; j < arr.Length - 2; j++)
22 | {
23 | sum = arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + arr[i + 1][j + 1] + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2];
24 | if (result < sum)
25 | {
26 | result = sum;
27 | }
28 | }
29 | }
30 | return result;
31 |
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/AlternatingCharacters.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Alternating_Characters
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/alternating-characters/problem
11 | /// Easy 20
12 | /// AAAA
13 | /// BBBBB
14 | /// ABABABAB
15 | /// BABABA
16 | /// AAABBB
17 | /// 3
18 | ///4
19 | ///0
20 | ///0
21 | ///4
22 | ///
23 | ///
24 | ///
25 | static int alternatingCharacters(string s)
26 | {
27 | int len = s.Length - 1;
28 | int i = 0;
29 | int count = 0;
30 | while (len > i)
31 | {
32 | //len--;
33 | if (s[len] == s[len - 1])
34 | {
35 |
36 | count++;
37 | }
38 | if (s[i] == s[i + 1] && i != len - 1 && i + 1 != len)
39 | {
40 |
41 | count++;
42 | }
43 | i++;
44 | len--;
45 |
46 | }
47 | return count;
48 |
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Apple_and_Orange.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Apple_and_Orange
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/apple-and-orange/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_3(String[] args)
15 | {
16 | string[] tokens_s = Console.ReadLine().Split(' ');
17 | int s = Convert.ToInt32(tokens_s[0]);
18 | int t = Convert.ToInt32(tokens_s[1]);
19 | string[] tokens_a = Console.ReadLine().Split(' ');
20 | int a = Convert.ToInt32(tokens_a[0]);
21 | int b = Convert.ToInt32(tokens_a[1]);
22 | string[] tokens_m = Console.ReadLine().Split(' ');
23 | int m = Convert.ToInt32(tokens_m[0]);
24 | int n = Convert.ToInt32(tokens_m[1]);
25 | string[] apple_temp = Console.ReadLine().Split(' ');
26 | int[] apple = Array.ConvertAll(apple_temp, Int32.Parse);
27 | string[] orange_temp = Console.ReadLine().Split(' ');
28 | int[] orange = Array.ConvertAll(orange_temp, Int32.Parse);
29 | int apple_count = 0;
30 | int orange_count = 0;
31 | for (int i_app = 0; i_app < m; i_app++)
32 | {
33 | if (a + apple[i_app] >= s && a + apple[i_app] <= t)
34 | {
35 | apple_count++;
36 | }
37 | }
38 | Console.WriteLine(apple_count);
39 | for (int i_ora = 0; i_ora < n; i_ora++)
40 | {
41 | if (b + orange[i_ora] >= s && b + orange[i_ora] <= t)
42 | {
43 | orange_count++;
44 | }
45 | }
46 | Console.WriteLine(orange_count);
47 |
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/ArrayManipulation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class ArrayManipulation
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/crush/problem
11 | /// 1 2 100
12 | ///2 5 100
13 | ///3 4 100
14 | ///o/p = 200;
15 | ///Hard 60
16 | ///
17 | ///
18 | ///
19 | ///
20 | public static long arrayManipulation(int n, int[][] queries)
21 | {
22 | int m = queries.Length;
23 | long[] numList = new long[n + 1];
24 | for (int i = 0; i < m; i++)
25 | {
26 | //string[] opString = Console.ReadLine().Split(' ');
27 | int a = queries[i][0];
28 | int b = queries[i][1];
29 | long k = queries[i][2];
30 | numList[a] += k;
31 | if (b + 1 <= n) numList[b + 1] -= k;
32 | }
33 | long tempMax = 0;
34 | long max = 0;
35 | for (int i = 1; i <= n; i++)
36 | {
37 | tempMax += numList[i];
38 | if (tempMax > max) max = tempMax;
39 | }
40 | return max;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/BubbleSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class BubbleSort
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/ctci-bubble-sort/problem
11 | ///
12 | ///
13 | static void countSwaps(int[] a)
14 | {
15 | int i, j, temp;
16 | bool swapped;
17 | int n = a.Length;
18 | int count = 0;
19 | for (i = 0; i < n - 1; i++)
20 | {
21 | swapped = false;
22 | for (j = 0; j < n - i - 1; j++)
23 | {
24 | if (a[j] > a[j + 1])
25 | {
26 | count++;
27 | // swap arr[j] and arr[j+1]
28 | temp = a[j];
29 | a[j] = a[j + 1];
30 | a[j + 1] = temp;
31 | swapped = true;
32 | }
33 | }
34 |
35 | // IF no two elements were
36 | // swapped by inner loop, then break
37 | if (swapped == false)
38 | break;
39 | }
40 | Console.WriteLine("Array is sorted in {0} swaps.", count);
41 |
42 | Console.WriteLine("First Element: {0}", a[0]);
43 | Console.WriteLine("Last Element: {0}", a[n - 1]);
44 |
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/CommonChild.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class CommonChild
8 | {
9 | ///
10 | /// s1=HARRY, s2=SALLY :o/p = 2
11 | /// s1 = AA, s2 BB :o/p = 0
12 | /// s1 = SHINCHAN, s2 = NOHARAAA : o/p=3
13 | /// s1 = ABCDEF, s2 = FBDAMN
14 | /// https://www.hackerrank.com/challenges/common-child/problem
15 | ///
16 | ///
17 | ///
18 | ///
19 | static int commonChild(string s1, string s2)
20 | {
21 |
22 | int m = s1.Length;
23 | int n = s2.Length;
24 | int[,] L = new int[m + 1, n + 1];
25 |
26 | for (int i = 0; i <= m; i++)
27 | {
28 | for (int j = 0; j <= n; j++)
29 | {
30 | if (i == 0 || j == 0)
31 | L[i, j] = 0;
32 | else if (s1[i - 1] == s2[j - 1])
33 | L[i, j] = L[i - 1, j - 1] + 1;
34 | else
35 | L[i, j] = Math.Max(L[i - 1, j], L[i, j - 1]);
36 | }
37 | }
38 | return L[m, n];
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Compare_the_Triplets.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Compare_the_Triplets
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/compare-the-triplets/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_10(String[] args)
15 | {
16 | string[] tokens_a0 = Console.ReadLine().Split(' ');
17 | int a0 = Convert.ToInt32(tokens_a0[0]);
18 | int a1 = Convert.ToInt32(tokens_a0[1]);
19 | int a2 = Convert.ToInt32(tokens_a0[2]);
20 | string[] tokens_b0 = Console.ReadLine().Split(' ');
21 | int b0 = Convert.ToInt32(tokens_b0[0]);
22 | int b1 = Convert.ToInt32(tokens_b0[1]);
23 | int b2 = Convert.ToInt32(tokens_b0[2]);
24 | int scoreA = 0;
25 | int scoreB = 0;
26 | if (a0 > b0)
27 | {
28 | scoreA = 1;
29 | }
30 |
31 | else if (a0 < b0)
32 | {
33 | scoreB = 1;
34 | }
35 |
36 |
37 | if (a1 > b1)
38 | {
39 | scoreA = scoreA + 1;
40 | }
41 |
42 | else if (a1 < b1)
43 | {
44 | scoreB = scoreB + 1;
45 | }
46 |
47 |
48 | if (a2 > b2)
49 | {
50 | scoreA = scoreA + 1;
51 | }
52 |
53 | else if (a2 < b2)
54 | {
55 | scoreB = scoreB + 1;
56 | }
57 | Console.WriteLine(scoreA + " " + scoreB);
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Diagonal_Difference.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Diagonal_Difference
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/diagonal-difference/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_9(String[] args)
15 | {
16 | int n = Convert.ToInt32(Console.ReadLine());
17 | int[][] a = new int[n][];
18 | for (int a_i = 0; a_i < n; a_i++)
19 | {
20 | string[] a_temp = Console.ReadLine().Split(' ');
21 | a[a_i] = Array.ConvertAll(a_temp, Int32.Parse);
22 | }
23 | int sumX = 0;
24 | int sumY = 0;
25 | int j = a.Length - 1;
26 | for (int i = 0; i < a.Length; i++)
27 | {
28 |
29 | sumX += a[i][i];
30 | sumY += a[j][i];
31 | j--;
32 |
33 | }
34 | int total = Math.Abs(sumX - sumY);
35 | Console.WriteLine(total);
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/FraudulentActivityNotifications.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class FraudulentActivityNotifications
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/fraudulent-activity-notifications/problem
11 | /// 9 5 and 2 3 4 2 3 6 8 4 5 o/p = 5
12 | /// 5 4 and 1 2 3 4 4 o/p = 0
13 | /// Medium 40
14 | ///
15 | ///
16 | ///
17 | ///
18 | static int activityNotifications(int[] expenditure, int d)
19 | {
20 | int[] freq = new int[201];
21 | int pop_element = 0;
22 | int notification = 0;
23 | int nextDaySpent = 0;
24 | for (int i = 0; i < d; i++)
25 | {
26 | freq[expenditure[i]]++;
27 | }
28 | for (int j = d; j < expenditure.Length; j++)
29 | {
30 | int median = getMedian(freq, d);
31 | pop_element = expenditure[j - d];
32 | freq[pop_element]--;
33 | freq[expenditure[j]]++;
34 | nextDaySpent = expenditure[j];
35 | if (d % 2 == 0)
36 | {
37 | if (expenditure[j] >= median)
38 | notification++;
39 | }
40 | else
41 | {
42 | if (expenditure[j] >= 2 * median)
43 | notification++;
44 | }
45 | }
46 | return notification;
47 | }
48 | public static int getMedian(int[] freq, int d)
49 | {
50 | int[] prefix_sum = new int[201];
51 | prefix_sum[0] = freq[0];
52 | for (int i = 1; i < 201; i++)
53 | {
54 | prefix_sum[i] = prefix_sum[i - 1] + freq[i];
55 | }
56 | int median;
57 | int a = 0;
58 | int b = 0;
59 | if (d % 2 == 0)
60 | {
61 | int first = d / 2;
62 | int second = first + 1;
63 | int i = 0;
64 | for (; i < 201; i++)
65 | {
66 | if (first <= prefix_sum[i])
67 | {
68 | a = i;
69 | break;
70 | }
71 | }
72 | for (; i < 201; i++)
73 | {
74 | if (second <= prefix_sum[i])
75 | {
76 | b = i;
77 | break;
78 | }
79 | }
80 |
81 | }
82 | else
83 | {
84 | int first = d / 2 + 1;
85 | for (int i = 0; i < 201; i++)
86 | {
87 | if (first <= prefix_sum[i])
88 | {
89 | a = i;
90 | break;
91 | }
92 | }
93 | }
94 | median = a + b;
95 | return median;
96 | }
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Grading_Students.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Grading_Students
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/grading/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | ///
15 | public static int multipleOf5(int G)
16 | {
17 | int aux = 0;
18 | if (G % 5 == 0)
19 | {
20 | return G;
21 | }
22 | else
23 | {
24 | while (G % 5 != 0)
25 | {
26 | G++;
27 | if (G % 5 == 0)
28 | {
29 | aux = G;
30 | }
31 | }
32 | }
33 | return aux;
34 | }
35 |
36 | static void Main_4(String[] args)
37 | {
38 | int n = Convert.ToInt32(Console.ReadLine());
39 | double result = 0;
40 | for (int a0 = 0; a0 < n; a0++)
41 | {
42 | int grade = Convert.ToInt32(Console.ReadLine());
43 | if (grade <= 37)
44 | {
45 | result = grade;
46 | }
47 | else if (grade >= 38)
48 | {
49 | //double d = double.Parse(grade.ToString());
50 | result = multipleOf5(grade);
51 | if (result - grade >= 3)
52 | {
53 | result = grade;
54 | }
55 | }
56 | Console.WriteLine(result);
57 | }
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/GridChallenge.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace CodingPractice.HackerRank
7 | {
8 | class GridChallenge
9 | {
10 | ///
11 | /// https://www.hackerrank.com/challenges/grid-challenge/problem
12 | /// Easy 20
13 | ///
14 | ///
15 | ///
16 | static string gridChallenge(string[] grid)
17 | {
18 | int[] count = new int[grid.Length];
19 | for (int i = 0; i < grid.Length; i++)
20 | {
21 | string test = sortedString(grid[i].ToArray());
22 | grid[i] = test;
23 | }
24 | //check column sortered or not
25 | for (int i = 0; i < grid.Length; i++)
26 | {
27 | for (int j = 0; j < grid[i].Length; j++)
28 | {
29 | count[i] = count[i] + (int)grid[i][j];
30 |
31 | }
32 | if (i > 0 && count[i - 1] > count[i])
33 | {
34 | return "NO";
35 | }
36 | else if (i > 0)
37 | {
38 | for (int k = 0; k < grid[i].Length; k++)
39 | {
40 | if (string.Compare(grid[i - 1][k].ToString(), grid[i][k].ToString()) > 0)
41 | {
42 | return "NO";
43 | }
44 | }
45 |
46 | }
47 |
48 | }
49 | return "YES";
50 |
51 | }
52 |
53 | private static string sortedString(char[] str)
54 | {
55 | Array.Sort(str);
56 | return string.Join("", str);
57 |
58 | }
59 |
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Insert_a_node_at_the_head_of_a_linked_list.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Insert_a_node_at_the_head_of_a_linked_list
8 | {
9 | public class Node
10 | {
11 | public int Data;
12 | public Node Next;
13 | }
14 | ///
15 | /// https://www.hackerrank.com/challenges/insert-a-node-at-the-head-of-a-linked-list/problem
16 | /// Easy 5
17 | ///
18 | ///
19 | ///
20 | ///
21 | public static Node Insert(Node head, int x)
22 | {
23 | Node newNode = new Node();
24 | newNode.Data = x;
25 | newNode.Next = head;
26 | return newNode;
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/IsThisABinarySearchTree.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class IsThisABinarySearchTree
8 | {
9 | public class Node
10 | {
11 | public int data;
12 | public Node left;
13 | public Node right;
14 | }
15 | ///
16 | /// https://www.hackerrank.com/challenges/ctci-is-binary-search-tree/problem
17 | /// Medium 30
18 | ///
19 | ///
20 | ///
21 | bool checkBST(Node root)
22 | {
23 | if (root == null)
24 | {
25 | return false;
26 | }
27 | return isBST(root, int.MinValue, int.MaxValue);
28 | }
29 | bool isBST(Node node, int min, int max)
30 | {
31 | if (node == null) return true;
32 | return min < node.data && node.data < max &&
33 | isBST(node.left, min, node.data) &&
34 | isBST(node.right, node.data, max);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/LeftRotation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class LeftRotation
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem
11 | /// Easy 20
12 | ///
13 | ///
14 | static void Main_1(String[] args)
15 | {
16 | string[] tokens_n = Console.ReadLine().Split(' ');
17 | int n = Convert.ToInt32(tokens_n[0]);
18 | int k = Convert.ToInt32(tokens_n[1]);
19 | string[] a_temp = Console.ReadLine().Split(' ');
20 | int[] a = Array.ConvertAll(a_temp, Int32.Parse);
21 |
22 | /* for (int j = 0; j < k; j++)
23 | {
24 | temp = a[0];
25 | for (int i = 1; i < a.Length ; i++)
26 | {
27 | a[i-1] = a[i];
28 | }
29 | a[a.Length-1] = temp;
30 | }*/
31 | int[] b = new int[a.Length];
32 |
33 | for (int i = 0; i < k; i++)
34 | {
35 | b[i] = a[(i + k) % n];
36 | }
37 |
38 | for (int j = k; j < a.Length; j++)
39 | {
40 | b[j] = a[(j + k) % n];
41 | }
42 |
43 | string value = string.Join(" ", b);
44 | Console.WriteLine(value);
45 |
46 |
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/LuckBalance.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace CodingPractice.HackerRank
7 | {
8 | class LuckBalance
9 | {
10 | ///
11 | /// https://www.hackerrank.com/challenges/luck-balance/problem
12 | /// Easy 20
13 | ///
14 | ///
15 | ///
16 | ///
17 | ///
18 | ///
19 | static int luckBalance(int n, int k, int[][] contests)
20 | {
21 | contests = contests.OrderByDescending(inner => inner[1]).ThenByDescending(inner => inner[0]).ToArray();
22 | int luckofSum = 0;
23 |
24 | //lose k no of contest
25 | for (int i = 0; i < n; i++)
26 | {
27 | if (contests[i][1] == 1 && k > i)
28 | {
29 | luckofSum = luckofSum + contests[i][0];
30 | }
31 | else if (contests[i][1] == 0)
32 | {
33 | luckofSum = luckofSum + contests[i][0];
34 | }
35 | else if (contests[i][1] == 1 && k <= i)
36 | {
37 | luckofSum = luckofSum - contests[i][0];
38 | }
39 | }
40 | return luckofSum;
41 | }
42 |
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/MarcsCakewalk.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class MarcsCakewalk
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/marcs-cakewalk/problem
11 | /// Easy 15
12 | ///
13 | ///
14 | ///
15 | static long marcsCakewalk(int[] calorie)
16 | {
17 | Array.Sort(calorie);
18 | Array.Reverse(calorie);
19 | long sum = 0;
20 | for (int i = 0; i < calorie.Length; i++)
21 | {
22 | sum = sum + (calorie[i] * (long)Math.Pow(2, i));
23 | }
24 | return sum;
25 |
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/MarkAndToys.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class MarkAndToys
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/mark-and-toys/problem
11 | /// 7 50 and 1 12 5 111 200 1000 10 o/p = 4
12 | ///
13 | ///
14 | ///
15 | ///
16 | static int maximumToys(int[] prices, int k)
17 | {
18 |
19 | quickSort(prices, 0, prices.Length - 1);
20 | int sum = 0;
21 | for (int i = 0; i < prices.Length; i++)
22 | {
23 | if (sum > k)
24 | {
25 | return i - 1;
26 | }
27 | sum = sum + prices[i];
28 | }
29 | return prices.Length - 1;
30 | }
31 |
32 | static void quickSort(int[] arr, int left, int right)
33 | {
34 | if (left >= right)
35 | {
36 | return;
37 | }
38 | int pivot = arr[(left + right) / 2];
39 | int index = partition(arr, left, right, pivot);
40 | quickSort(arr, left, index - 1);
41 | quickSort(arr, index, right);
42 | }
43 | static int partition(int[] arr, int left, int right, int pivot)
44 | {
45 | while (left <= right)
46 | {
47 | while (arr[left] < pivot)
48 | {
49 | left++;
50 | }
51 | while (arr[right] > pivot)
52 | {
53 | right--;
54 | }
55 | if (left <= right)
56 | {
57 | int temp = arr[left];
58 | arr[left] = arr[right];
59 | arr[right] = temp;
60 | left++;
61 | right--;
62 | }
63 | }
64 | return left;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/MaximumPerimeterTriangle.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class MaximumPerimeterTriangle
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/maximum-perimeter-triangle/problem
11 | ///
12 | ///
13 | ///
14 | static int[] maximumPerimeterTriangle(int[] sticks)
15 | {
16 | Array.Sort(sticks);
17 | int i = sticks.Length - 3;
18 | while (i >= 0 && (sticks[i] + sticks[i + 1] <= sticks[i + 2]))
19 | {
20 | i -= 1;
21 | }
22 |
23 |
24 | if (i >= 0)
25 | return new int[] { sticks[i], sticks[i + 1], sticks[i + 2] };
26 | else
27 | return new int[] { -1 };
28 |
29 |
30 | }
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Mini_Max_Sum.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Mini_Max_Sum
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/mini-max-sum/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_2(String[] args)
15 | {
16 | string[] arr_temp = Console.ReadLine().Split(' ');
17 | int[] arr = Array.ConvertAll(arr_temp, Int32.Parse);
18 | //long[] number = new long[5];
19 | long sum = 0, min = arr[0], max = 0;
20 | for (int i = 0; i < arr.Length; i++)
21 | {
22 | if (arr[i] > max)
23 | max = arr[i];
24 | else if (arr[i] < min)
25 | min = arr[i];
26 | sum += arr[i];
27 | }
28 |
29 | Console.WriteLine((sum - max) + " " + (sum - min));
30 | }
31 |
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/MinimumSwaps2.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class MinimumSwaps2
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/minimum-swaps-2/problem
11 | /// Medidum
12 | /// 40
13 | /// 4 3 1 2 o/p = 3
14 | /// 2 3 4 1 5 o/p = 3
15 | /// 1 3 5 2 4 6 7 0/p= 3
16 | ///
17 | ///
18 | ///
19 | static int minimumSwaps(int[] arr)
20 | {
21 | int i = 0;
22 | int swapCount = 0;
23 | while (i < arr.Length - 1)
24 | {
25 | while (arr[i] != i + 1)
26 | {
27 | int swapKey = arr[i] - 1;
28 | int temp = arr[i];
29 | arr[i] = arr[swapKey];
30 | arr[swapKey] = temp;
31 | swapCount++;
32 | }
33 | i++;
34 |
35 |
36 | }
37 | return swapCount;
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Minimum_Absolute_Difference_in_an_Array.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Minimum_Absolute_Difference_in_an_Array
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/minimum-absolute-difference-in-an-array/problem
11 | /// Easy 15
12 | ///
13 | ///
14 | ///
15 | ///
16 | static int minimumAbsoluteDifference(int n, int[] arr)
17 | {
18 | Array.Sort(arr);
19 | int min = Math.Abs(arr[0] - arr[1]);
20 | for (int i = 0; i < n - 1; i++)
21 | {
22 | if (min > Math.Abs(arr[i] - arr[i + 1]))
23 | {
24 | min = Math.Abs(arr[i] - arr[i + 1]);
25 | }
26 | }
27 | return min;
28 |
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/NewYearChaos.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class NewYearChaos
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/new-year-chaos/problem
11 | /// Medium 40
12 | ///
13 | ///
14 | // Complete the minimumBribes function below.
15 | static void minimumBribes(int[] q)
16 | {
17 | int n = q.Length;
18 | int i, j, temp;
19 | bool swapped;
20 | Dictionary res = new Dictionary();
21 | int total = 0;
22 | for (i = 0; i < n - 1; i++)
23 | {
24 | swapped = false;
25 | for (j = 0; j < n - i - 1; j++)
26 | {
27 | if (q[j] > q[j + 1])
28 | {
29 |
30 | total++;
31 | if (!res.ContainsKey(q[j]))
32 | {
33 | res.Add(q[j], 1);
34 | }
35 | else
36 | {
37 | res[q[j]]++;
38 |
39 | if (res[q[j]] >= 3)
40 | {
41 | Console.WriteLine("Too chaotic");
42 | return;
43 | }
44 | }
45 |
46 | temp = q[j];
47 | q[j] = q[j + 1];
48 | q[j + 1] = temp;
49 | swapped = true;
50 | }
51 | }
52 |
53 | // IF no two elements were
54 | // swapped by inner loop, then break
55 | if (swapped == false)
56 | break;
57 | }
58 | Console.WriteLine(total);
59 |
60 | }
61 |
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/OpenCloseSymbolsCheck.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | public class OpenCloseSymbolsCheck
8 | {
9 | public static bool checkOpenClose()
10 | {
11 | string str = "{[[(())]}}}";
12 | bool flag = true;
13 | bool result = true; ;
14 | while (flag)
15 | {
16 | if (str.Length > 0)
17 | {
18 | switch (str[0])
19 | {
20 | case '{':
21 | {
22 | if (str[str.Length - 1] == '}')
23 | {
24 | str = str.Remove(0, 1);
25 | str = str.Remove(str.Length - 1, 1);
26 |
27 | }
28 | else
29 | {
30 | result = false;
31 | return false;
32 | }
33 | break;
34 | }
35 | case '[':
36 | {
37 | if (str[str.Length - 1] == ']')
38 | {
39 | str = str.Remove(0, 1);
40 | str = str.Remove(str.Length - 1, 1);
41 | }
42 | else
43 | {
44 | result = false;
45 | return false;
46 | }
47 | break;
48 | }
49 | case '(':
50 | {
51 | if (str[str.Length - 1] == ')')
52 | {
53 | str = str.Remove(0, 1);
54 | str = str.Remove(str.Length - 1, 1);
55 | }
56 | else
57 | {
58 | result = false;
59 | return false;
60 | }
61 | break;
62 | }
63 | default:
64 | {
65 | return true;
66 | }
67 | }
68 | }
69 | else
70 | {
71 | flag = false;
72 | }
73 | //return false;
74 | }
75 | return result;
76 |
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Plus_Minus.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Plus_Minus
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/plus-minus/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_8(String[] args)
15 | {
16 | int n = Convert.ToInt32(Console.ReadLine());
17 | string[] arr_temp = Console.ReadLine().Split(' ');
18 | int[] arr = Array.ConvertAll(arr_temp, Int32.Parse);
19 | double zero = 0;
20 | double pos = 0;
21 | double neg = 0;
22 | double zeroSum = 0;
23 | double posSum = 0;
24 | double negSum = 0;
25 | int totalLen = arr.Length;
26 | for (int i = 0; i < arr.Length; i++)
27 | {
28 | if (arr[i] == 0)
29 | {
30 | zero = zero + 1;
31 | }
32 | else if (arr[i] < 0)
33 | {
34 | neg = neg + 1;
35 | }
36 | else if (arr[i] > 0)
37 | {
38 | pos = pos + 1;
39 | }
40 |
41 | }
42 | zeroSum = zero / totalLen;
43 | posSum = pos / totalLen;
44 | negSum = neg / totalLen;
45 |
46 | Console.WriteLine(Math.Round(posSum, 6));
47 | Console.WriteLine(Math.Round(negSum, 6));
48 | Console.WriteLine(Math.Round(zeroSum, 6));
49 | }
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Print_the_Elements_of_a_Linked_List.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Print_the_Elements_of_a_Linked_List
8 | {
9 | public class Node
10 | {
11 | public int Data;
12 | public Node Next;
13 | }
14 | ///
15 | /// https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem
16 | /// Easy 5
17 | ///
18 | ///
19 | public static void Print(Node head)
20 | {
21 | while (head != null)
22 | {
23 | Console.WriteLine(head.Data);
24 | head = head.Next;
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/RansomNote.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class RansomNote
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/ctci-ransom-note/problem
11 | /// "give me one grand today night" note := give one grand today ,o/p YES
12 | /// "two times three is not four" note := two times two is four o/p = No
13 | /// "ive got a lovely bunch of coconuts" note:= "ive got some coconuts" o/p = No
14 | ///
15 | ///
16 | ///
17 | static void checkMagazine(string[] magazine, string[] note)
18 | {
19 | //magazine
20 | //note
21 | Dictionary counter = new Dictionary();
22 | foreach (string str in magazine)
23 | {
24 | if (!counter.ContainsKey(str))
25 | {
26 | counter.Add(str, 1);
27 | }
28 | else
29 | {
30 | counter[str] = counter[str] + 1;
31 | }
32 |
33 | }
34 | foreach (string str in note)
35 | {
36 | if (counter.ContainsKey(str))
37 | {
38 | counter[str] = counter[str] - 1;
39 | if (counter[str] < 0)
40 | {
41 | Console.WriteLine("No");
42 | return;
43 | }
44 | }
45 | else
46 | {
47 | Console.WriteLine("No");
48 | return;
49 | }
50 | }
51 | Console.WriteLine("Yes");
52 |
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/SherlockAndAnagrams.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 |
6 | namespace CodingPractice.HackerRank
7 | {
8 | class SherlockAndAnagrams
9 | {
10 | ///
11 | /// abba o/p = 4
12 | /// abcd o/p = 0
13 | /// ifailuhkqq o/p = 3
14 | /// kkkk o/p = 10
15 | /// cdcd o/p = 5
16 | ///
17 | ///
18 | ///
19 | static int sherlockAndAnagrams(string s)
20 | {
21 | int count = 0;
22 |
23 | for (int i = 0; i < s.Length; i++)
24 | {
25 |
26 | for (int j = 1; j < s.Length - i; j++)
27 | {
28 | string str = s.Substring(i, j);
29 |
30 | byte[] bytes = Encoding.ASCII.GetBytes(str);
31 | int sum = bytes.Sum(d => d);
32 | //char[] strArr = str.ToCharArray();
33 | // Array.Sort(strArr);
34 | compare(str, s, ref count, sum, i + 1);
35 | }
36 |
37 | }
38 | return count;
39 |
40 | }
41 | private static void compare(string str, string compare, ref int count, int sum, int len)
42 | {
43 | string original = "";
44 | string compareStr = "";
45 | int n = str.Length;
46 | for (int j = len; j < compare.Length; j = j + 1)
47 | {
48 | if (j + n <= compare.Length && j > 0)
49 | {
50 | string sub = compare.Substring(j, n);
51 | // char[] strCompareArr = sub.ToCharArray();
52 | // Array.Sort(strCompareArr);
53 | //original = string.Join("", strArr);
54 | //compareStr = string.Join("", strCompareArr);
55 | byte[] bytescompare = Encoding.ASCII.GetBytes(sub);
56 | int sumCompare = bytescompare.Sum(d => d);
57 | if (sum == sumCompare)
58 | {
59 | if (sub == str)
60 | {
61 | count++;
62 | }
63 | else if (areMetaStrings(sub, str))
64 | {
65 | count++;
66 | }
67 | }
68 | }
69 |
70 |
71 | }
72 |
73 | }
74 | static bool areMetaStrings(String str1,
75 | String str2)
76 | {
77 | int[] arrA = new int[26];
78 | int[] arrB = new int[26];
79 | for (int i = 0; i < str1.Length; i++)
80 | {
81 | arrA[str1[i] - 'a']++;
82 | }
83 | for (int i = 0; i < str2.Length; i++)
84 | {
85 | arrB[str2[i] - 'a']++;
86 | }
87 | for (int i = 0; i < 26; i++)
88 | {
89 | if (arrA[i] != arrB[i])
90 | {
91 | return false;
92 | }
93 | }
94 | return true;
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/SherlockAndTheValidString.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class SherlockAndTheValidString
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem
11 | /// s = aabbcd, o/p = NO
12 | /// s = aabbccddeefghi, o/p = NO
13 | /// s = abcdefghhgfedecba, o/p = YES
14 | ///
15 | ///
16 | ///
17 | ///
18 | static string isValid(string s)
19 | {
20 | int[] chars = new int[26];
21 | int count = 0;
22 | for (int i = 0; i < s.Length; i++)
23 | {
24 | chars[s[i] - 'a']++;
25 | }
26 | int temp = chars[0];
27 | for (int i = 1; i < 25; i++)
28 | {
29 | if (temp != chars[i] && chars[i] != 0)
30 | {
31 | if (chars[i] > temp)
32 | {
33 | if (chars[i] - 1 == temp || chars[i] - 1 > temp)
34 | {
35 | count++;
36 | }
37 | }
38 | else
39 | {
40 | if (chars[i] - 1 == 0)
41 | {
42 | count++;
43 | }
44 | else if (chars[i] - 1 != temp)
45 | {
46 | count++;
47 | }
48 |
49 | }
50 | }
51 | if (count > 1)
52 | {
53 | return "NO";
54 | }
55 |
56 | }
57 | return "YES";
58 |
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/SliceDown.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | /*
8 |
9 | Input: array of integer value
10 |
11 | Output: print each iteration by subtracting the smallest elements till array becomes empty
12 |
13 | For example:
14 | For input array: 4, 3, 5, 8, 12
15 |
16 | At each iteration, find minimum number and subtract it from element and print non zero elements
17 | 1 2 5 9 (Subtracted 3 from each elements)
18 | 1 4 8 (Subtracted 1 from each elements)
19 | 3 7 (Subtracted 1 from each elements)
20 | 4 (Subtracted 3 from each elements)
21 |
22 | For the above problem, naive approach is to find the minimum in each iteration, subtracting it and copy it in new memory.
23 |
24 | There are 2 place where you can improve on memory as well as the time complexity (less iteration).
25 |
26 | 1. Identify minimum value while subtracting the minimum value
27 |
28 | 2. Do not copy element to another array, just simple store the last index value and decrease it in recursive call.
29 | */
30 | public class SliceDown
31 | {
32 | static void Main_1(string[] args)
33 | {
34 | SliceDown sliceDown = new SliceDown();
35 | int[] a = { 15, 12, 10, 5, 3 };
36 | int smallNumber = sliceDown.FindSmallest(a);
37 | sliceDown.sliceDown(a, a.Length, smallNumber);
38 | Console.ReadLine();
39 | }
40 |
41 | private void sliceDown(int[] a, int range, int smallestIndex)
42 | {
43 |
44 | int new_position = 0;
45 | int smallNumber = FindSmallest(a);
46 | int new_smallestIndex = -1;
47 | for (int i = 0; i < a.Length; i++)
48 | {
49 | if (smallNumber != a[i])
50 | {
51 | a[new_position] = a[i] - smallNumber;
52 | if (new_smallestIndex < 0)
53 | {
54 | new_smallestIndex = new_position;
55 | }
56 | else if (a[new_position] < a[new_smallestIndex])
57 | {
58 | new_smallestIndex = new_position;
59 | }
60 |
61 | new_position++;
62 | }
63 | }
64 | printArray(a, new_position);
65 | if (new_position > 1)
66 | {
67 | sliceDown(a, new_position, new_smallestIndex);
68 | }
69 |
70 | }
71 | private static void printArray(int[] a, int range)
72 | {
73 | for (int i = 0; i < range; i++)
74 | {
75 | Console.WriteLine(a[i] + " ");
76 | }
77 |
78 | }
79 |
80 | private int FindSmallest(int[] input)
81 | {
82 | int smallestIndex = 0;
83 | for (int i = 0; i < input.Length; i++)
84 | {
85 | if (input[smallestIndex] > input[i])
86 | {
87 | smallestIndex = i;
88 | }
89 | }
90 | return smallestIndex;
91 | }
92 |
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/SpecialPalindromeAgain.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Text;
5 |
6 | namespace CodingPractice.HackerRank
7 | {
8 | class SpecialPalindromeAgain
9 | {
10 | ///
11 | /// https://www.hackerrank.com/challenges/special-palindrome-again/problem
12 | /// str = asasd, o/p = 7
13 | /// str = abcbaba , o/p = 10
14 | /// str = aaaa , o/p = 10
15 | ///
16 | ///
17 | ///
18 | ///
19 | static long substrCount(int n, string str)
20 | {
21 | // create an empty set to store all palindromic substrings
22 | ArrayList set = new ArrayList();
23 |
24 | for (int i = 0; i < str.Length; i++)
25 | {
26 | // find all odd length palindrome with str[i] as mid point
27 |
28 | expandSearch(str, i, i, set);
29 |
30 | // find all even length palindrome with str[i] and str[i+1]
31 | // as its mid points
32 | expandSearch(str, i, i + 1, set);
33 | }
34 | long count = set.Count + str.Length;
35 | return count;
36 | }
37 | private static bool allCharactersSame(string s)
38 | {
39 | int n = s.Length;
40 | for (int i = 1; i < n; i++)
41 | if (s[i] != s[0])
42 | return false;
43 |
44 | return true;
45 | }
46 | private static void expandSearch(String str, int low, int high, ArrayList set)
47 | {
48 | string palindrome = string.Empty;
49 | String left = string.Empty;
50 | String right = string.Empty;
51 | while (low >= 0 && high < str.Length && str[low] == str[high])
52 | {
53 |
54 | // if(str.Substring())
55 | palindrome = str.Substring(low, high - low + 1);
56 | // if (compare.Length > 3 && compare.Length % 2 !=0) // compare between char from mid
57 | {
58 | left = palindrome.Substring(0, palindrome.Length / 2);
59 | right = palindrome.Substring(palindrome.Length - left.Length);
60 | }
61 | if (left == right && palindrome.Length > 1 && allCharactersSame(left) == true && allCharactersSame(right) == true) // uncomment to make it normal palindrome
62 | set.Add(palindrome);
63 | low--;
64 | high++;
65 |
66 | }
67 |
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Staircase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Staircase
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/staircase/problem
11 | /// Easy 10
12 | ///
13 | ///
14 | static void Main_7(String[] args)
15 | {
16 | int n = Convert.ToInt32(Console.ReadLine());
17 | string pad = "#";
18 | string padResult = "";
19 | for (int i = 1; i <= n; i++)
20 | {
21 | padResult = pad.PadLeft(i, '#');
22 | padResult = padResult.PadLeft(n);
23 | Console.WriteLine(padResult);
24 | }
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/TheMaximumSubarray.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class TheMaximumSubarray
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/maxsubarray/problem
11 | ///
12 | ///
13 | ///
14 | static int[] maxSubarray(int[] A)
15 | {
16 |
17 | int[] result = new int[2];
18 | int size = A.Length;
19 | int max_so_far = int.MinValue,
20 | max_ending_here = 0;
21 | int totalsum = 0;
22 | bool isPositive = false;
23 | for (int i = 0; i < size; i++)
24 | {
25 | max_ending_here = max_ending_here + A[i];
26 |
27 | if (max_so_far < max_ending_here)
28 | {
29 | max_so_far = max_ending_here;
30 | }
31 | if (A[i] > 0)
32 | {
33 | totalsum += A[i];
34 | isPositive = true;
35 | }
36 |
37 | if (max_ending_here < 0)
38 | max_ending_here = 0;
39 | }
40 | if (isPositive == false)
41 | {
42 | Array.Sort(A);
43 | Array.Reverse(A);
44 | result[0] = A[0];
45 | result[1] = A[0];
46 | }
47 | else
48 | {
49 | result[0] = max_so_far;
50 | result[1] = totalsum;
51 | }
52 | return result;
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/Time_Conversion.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class Time_Conversion
8 | {
9 | ///
10 | /// https://www.hackerrank.com/challenges/time-conversion/problem
11 | /// Easy 15
12 | ///
13 | ///
14 | static void Main_6(String[] args)
15 | {
16 | string time = Console.ReadLine();
17 |
18 | DateTime dt;
19 | bool res = DateTime.TryParse(time, out dt);
20 | time = dt.ToString("HH:mm:ss");
21 | Console.WriteLine(time);
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/TwoStrings.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | class TwoStrings
8 | {
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/HackerRank/WordExclude.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.HackerRank
6 | {
7 | public class WordExclude
8 | {
9 | public static void retrieveWords()
10 | {
11 |
12 | }
13 |
14 | public static List retrieveWords(string literalWords,List execludeWords)
15 | {
16 | //Replace
17 | List commonWordsToExeclude = new List();
18 |
19 | string noSpecialChar = System.Text.RegularExpressions.Regex.Replace(literalWords, @"[^0-9a-zA-Z]+", " ");
20 |
21 | string modifiedWords = "";
22 | foreach (string word in execludeWords)
23 | {
24 | modifiedWords = noSpecialChar.Replace(word, "");
25 |
26 | }
27 | string[] list_Words = modifiedWords.Split(' ');
28 |
29 | //words counts
30 | // list_Words.
31 | return null;
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/Leetcode/Paint-House.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.Leetcode
6 | {
7 | class Paint_House
8 | {
9 |
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/OnlineProblem/CareerCup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.OnlineProblem
6 | {
7 | public class CareerCup
8 | {
9 | public static Dictionary GenerateSet()
10 | {
11 | // https://www.careercup.com/question?id=5095657344860160
12 | //int[,] set = { { 1, 2 }, { 1, 3 }, { 2, 4 } }; // 1,3 & 2, 4
13 | // int[,] set = { { 1, 2 }, { 1, 3 }, { 1, 1 }, { 2, 4 },{ 1, 5 },{ 5, 1 },{ 5, 4 } };
14 | //1,5 & 2,4 & 5,4
15 | int[,] set = { { 1, 2 }, { 1, 3 }, { 1, 1 }, { 2, 4 },{ 1, 5 },{ 4, 9 },{ 5, 4 },{ 2, 8 } };
16 | //1,5 & 2,8 & 5,4 & 4,9
17 | Dictionary map = new Dictionary();
18 |
19 | for(int i = 0; i < set.GetLength(0); i++)
20 | {
21 | if (!map.ContainsKey(set[i, 0]) && !map.ContainsKey(set[i, 1]) && set[i, 0] != set[i, 1])
22 | {
23 | map.Add(set[i, 0], set[i, 1]);
24 | map.Add(set[i, 1], set[i, 0]);
25 | }
26 | else if (map.ContainsKey(set[i, 0]) && map[set[i, 0]] < set[i, 1])
27 | {
28 | map[set[i, 0]] = set[i, 1];
29 | }
30 | if (!map.ContainsKey(set[i, 0]))
31 | {
32 | map.Add(set[i, 0], set[i, 1]);
33 | }
34 | if (!map.ContainsKey(set[i, 1]))
35 | {
36 | map.Add(set[i, 1], 0);
37 | }
38 |
39 | }
40 | Dictionary result = new Dictionary();
41 | foreach (KeyValuePair key in map)
42 | {
43 | if (key.Value != 0)
44 | {
45 | result.Add(key.Key, key.Value);
46 | }
47 | }
48 | return result;
49 | }
50 |
51 | public static List getIntersection(int[] arr1, int[] arr2)
52 | {
53 | List list = new List();
54 | for (int i = 0; i < arr1.Length; i++)
55 | {
56 | for (int j = 0; j < arr2.Length; j++)
57 | {
58 | if (arr1[i] == arr2[j])
59 | {
60 | list.Add(arr1[i]);
61 | }
62 | }
63 | }
64 | return list;
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/AmazonDemo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | public class AmazonDemo
8 | {
9 | public static void generalizedGSD()
10 | {
11 | int[] arr = { 2, 4, 6, 8, 10 };
12 | generalizedGSD(arr.Length - 1, arr);
13 |
14 | }
15 | public static int generalizedGSD(int num,int[] arr)
16 | {
17 | bool isOdd = false;
18 | bool isEven = false;
19 | int initialVal = arr[0];
20 |
21 | for(int i = 1; i < num-1; i++)
22 | {
23 | if (arr[i] % 2 == 0)
24 | {
25 | isEven = true;
26 | }
27 | else
28 | {
29 | isOdd = true;
30 | }
31 | if(isEven && isOdd)
32 | {
33 | return 1;
34 | }
35 | if (arr[i]% initialVal==0)
36 | {
37 |
38 | }
39 | else
40 | {
41 | for(int j = arr[i] - initialVal; j >=2;j = j- 2)
42 | {
43 | if (arr[i] % j == 0)
44 | {
45 | return j;
46 | }
47 | }
48 | }
49 | }
50 |
51 | return 0;
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/BubbleSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | ///
8 | /// Worst-case performance O(n^{2})
9 | /// Best-case performance O(n)
10 | /// Average performance O(n^{2})
11 | /// Worst-case space complexity O(1) auxiliary
12 | /// https://en.wikipedia.org/wiki/Bubble_sort
13 | ///
14 | public class BubbleSort
15 | {
16 | ///
17 | /// compares each pair of adjacent items and swaps them if they are in the wrong order.
18 | /// The pass through the list is repeated until no swaps are needed,
19 | /// which indicates that the list is sorted
20 | ///
21 | public void bubbleSort(int[] number)
22 | {
23 | // int[] number = { 89, 76, 45, 92, 67, 12, 99 };
24 | bool flag = true;
25 | int temp;
26 | int numLength = number.Length;
27 | //sorting an array
28 | for (int i = 1; (i <= (numLength - 1)) && flag; i++)
29 | {
30 | flag = false;
31 | for (int j = 0; j < (numLength - 1); j++)
32 | {
33 | if (number[j + 1] < number[j])
34 | {
35 | temp = number[j];
36 | number[j] = number[j + 1];
37 | number[j + 1] = temp;
38 | flag = true;
39 | }
40 | }
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/HeapSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | class HeapSort
8 | {
9 | public void sort(int[] arr)
10 | {
11 | int len = arr.Length;
12 |
13 | // Build heap (rearrange array)
14 | for (int i = len / 2 - 1; i >= 0; i--)
15 | heapify(arr, len, i);
16 |
17 | // One by one extract an element from heap
18 | for (int i = len - 1; i >= 0; i--)
19 | {
20 | // Move current root to end
21 | int temp = arr[0];
22 | arr[0] = arr[i];
23 | arr[i] = temp;
24 |
25 | // call max heapify on the reduced heap
26 | heapify(arr, i, 0);
27 | }
28 | }
29 |
30 | // To heapify a subtree rooted with node i which is
31 | // an index in arr[]. n is size of heap
32 | void heapify(int[] arr, int n, int i)
33 | {
34 | int largest = i; // Initialize largest as root
35 | int l = 2 * i + 1; // left = 2*i + 1
36 | int r = 2 * i + 2; // right = 2*i + 2
37 |
38 | // If left child is larger than root
39 | if (l < n && arr[l] > arr[largest])
40 | largest = l;
41 |
42 | // If right child is larger than largest so far
43 | if (r < n && arr[r] > arr[largest])
44 | largest = r;
45 |
46 | // If largest is not root
47 | if (largest != i)
48 | {
49 | int swap = arr[i];
50 | arr[i] = arr[largest];
51 | arr[largest] = swap;
52 |
53 | // Recursively heapify the affected sub-tree
54 | heapify(arr, n, largest);
55 | }
56 | }
57 |
58 | /* A utility function to print array of size n */
59 | static void printArray(int[] arr)
60 | {
61 | int n = arr.Length;
62 | for (int i = 0; i < n; ++i)
63 | Console.Write(arr[i] + " ");
64 | Console.Read();
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/InsertionSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | ///
8 | /// Worst-case performance О(n2) comparisons, swaps
9 | /// Best-case performance O(n) comparisons, O(1) swaps
10 | /// Average performance О(n2) comparisons, swaps
11 | /// Worst-case space complexity О(n) total, O(1) auxiliary
12 | /// https://en.wikipedia.org/wiki/Insertion_sort
13 | /// Each iteration, insertion sort removes one element from the input data,
14 | /// finds the location it belongs within the sorted list, and inserts it there.
15 | /// It repeats until no input elements remain.
16 | ///
17 | public class InsertionSort
18 | {
19 | /* Function to sort an array using insertion sort*/
20 | public void insertionSort(int[] arr, int n)
21 | {
22 | int i, key, j;
23 | for (i = 1; i < n; i++)
24 | {
25 | key = arr[i];
26 | j = i - 1;
27 |
28 | /* Move elements of arr[0..i-1], that are
29 | greater than key, to one position ahead
30 | of their current position */
31 | while (j >= 0 && arr[j] > key)
32 | {
33 | arr[j + 1] = arr[j];
34 | j = j - 1;
35 | }
36 | arr[j + 1] = key;
37 | }
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/Lexicographical.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 |
6 | namespace CodingPractice.SortingAlgorithm
7 | {
8 | class Lexicographical
9 | {
10 |
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/MergeSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | ///
8 | /// Worst-case performance O(n log n)
9 | /// Best-case performance O(n log n) typical,
10 | /// Average performance O(n log n)
11 | /// Worst-case space complexity О(n) total, O(n)
12 | /// https://en.wikipedia.org/wiki/Merge_sort
13 | ///
14 | public class MergeSort
15 | {
16 | // Main function that sorts arr[l..r] using
17 | // merge()
18 | ///
19 | ///
20 | ///
21 | ///
22 | ///
23 | ///
24 | public void mergeSort(int[] arr, int l, int r)
25 | {
26 | //int arr[] = {12, 11, 13, 5, 6, 7};
27 |
28 | if (l < r)
29 | {
30 | // Find the middle point
31 | int m = (l + r) / 2;
32 |
33 | // Sort first and second halves
34 | mergeSort(arr, l, m);
35 | mergeSort(arr, m + 1, r);
36 |
37 | // Merge the sorted halves
38 | merge(arr, l, m, r);
39 | }
40 | }
41 |
42 |
43 | ///
44 | /// Merges two subarrays of arr[].
45 | /// First subarray is arr[l..m]
46 | /// Second subarray is arr[m+1..r]
47 | /// http://quiz.geeksforgeeks.org/merge-sort/
48 | ///
49 | public void merge(int[] arr, int l, int m, int r)
50 | {
51 | // Find sizes of two subarrays to be merged
52 | int n1 = m - l + 1;
53 | int n2 = r - m;
54 | int i = 0, j = 0;
55 | /* Create temp arrays */
56 | int[] L = new int[n1];
57 | int[] R = new int[n2];
58 |
59 | /*Copy data to temp arrays*/
60 | for (i = 0; i < n1; ++i)
61 | L[i] = arr[l + i];
62 | for (j = 0; j < n2; ++j)
63 | R[j] = arr[m + 1 + j];
64 |
65 |
66 | /* Merge the temp arrays */
67 |
68 | // Initial indexes of first and second subarrays
69 | i = 0;
70 | j = 0;
71 |
72 | // Initial index of merged subarry array
73 | int k = l;
74 | while (i < n1 && j < n2)
75 | {
76 | if (L[i] <= R[j])
77 | {
78 | arr[k] = L[i];
79 | i++;
80 | }
81 | else
82 | {
83 | arr[k] = R[j];
84 | j++;
85 | }
86 | k++;
87 | }
88 |
89 | /* Copy remaining elements of L[] if any */
90 | while (i < n1)
91 | {
92 | arr[k] = L[i];
93 | i++;
94 | k++;
95 | }
96 |
97 | /* Copy remaining elements of L[] if any */
98 | while (j < n2)
99 | {
100 | arr[k] = R[j];
101 | j++;
102 | k++;
103 | }
104 | }
105 |
106 |
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/QuickSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | ///
8 | /// Quicksort is a comparison sort
9 | /// Quicksort is a divide and conquer algorithm.
10 | /// Quicksort first divides a large array into two smaller sub-arrays:
11 | /// the low elements and the high elements. Quicksort can then recursively sort the sub-arrays.
12 | ///
13 | /// Average performance O(n log n)
14 | /// Best-case performance O(n log n) (simple partition)
15 | /// Worst-case performance O(n2)
16 | /// http://www.algolist.net/Algorithms/Sorting/Quicksort
17 | /// Worst-case space complexity O(n) auxiliary (naive)
18 | ///
19 | public class QuickSort
20 | {
21 | /************
22 | * Step 1 − Choose the highest index value has pivot
23 | Step 2 − Take two variables to point left and right of the list excluding pivot
24 | Step 3 − left points to the low index
25 | Step 4 − right points to the high
26 | Step 5 − while value at left is less than pivot move right
27 | Step 6 − while value at right is greater than pivot move left
28 | Step 7 − if both step 5 and step 6 does not match swap left and right
29 | Step 8 − if left ≥ right, the point where they met is new pivot
30 | */
31 |
32 | int partition(int[] arr, int left, int right)
33 | {
34 | int i = left, j = right;
35 | int tmp;
36 | int pivot = arr[(left + right) / 2];
37 |
38 | while (i <= j)
39 | {
40 | while (arr[i] < pivot)
41 | i++;
42 | while (arr[j] > pivot)
43 | j--;
44 | if (i <= j)
45 | {
46 | tmp = arr[i];
47 | arr[i] = arr[j];
48 | arr[j] = tmp;
49 | i++;
50 | j--;
51 | }
52 | };
53 |
54 | return i;
55 | }
56 |
57 | public void quickSort(int[] arr, int left, int right)
58 | {
59 |
60 | if (left < right)
61 | {
62 | int index = partition(arr, left, right);
63 | //if (left < index - 1)
64 | quickSort(arr, left, index - 1);
65 | //if (index < right)
66 | quickSort(arr, index+1, right);
67 | }
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/README.MD:
--------------------------------------------------------------------------------
1 | # Sorting
2 |
3 | A sorting algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order.
4 |
5 |
6 | #### Time Complexity
7 |
8 | | Sorting Algorithm | Best | Average | Worst |
9 | | --- | --- | --- | --- |
10 | | Quicksort Array | O(n log(n)) | O(n log(n)) | O(n2) |
11 | | Mergesort Array | O(n log(n)) | O(n log(n)) | O(n log(n)) |
12 | | Heapsort Array | O(n log(n)) | O(n log(n)) | O(n log(n)) |
13 | | Bubble Sort | O(n) | O(n2) | O(n2) |
14 | | Insertion Sort | O(n) | O(n2) | O(n2) |
15 | | Select Sort | O(n2) | O(n2) | O(n2) |
16 |
17 | #### Why is sorting so important
18 | - The first step in organizing data is sorting. Lots of tasks become easier once a data set of items is sorted
19 | - Some algorithms like binary search are built around a sorted data structure.
20 |
21 | #### Considerations:
22 | - How to sort: descending order or ascending order?
23 | - Sorting based on what? An object name (alphabetically), by some number defined by its fields/instance variables. Or maybe compare dates, birthdays, etc.
24 | - What happens with equals keys, for example various people with the same name: John, then sort them by Last Name.
25 | - Does your sorting algorithm sorts in place or needs extra memory to hold another copy of the array to be sorted. This is even more important in embedded systems.
26 |
27 | #### SELECTION SORT
28 |
29 | It is called selection sort because it repeatedly selects the smallest remaining item:
30 | 1. Find the smallest element. Swap it with the first element.
31 | 2. Find the second smallest element. Swap it with the second element
32 | 3. Find the third smallest element. Swap it with the third element
33 | 4. Repeat finding the smallest element and swapping in the correct position until the list is sorted
34 |
35 | #### MERGESORT
36 |
37 | Mergesort is also called divide and conquer algorithm, because it divides the original data into smaller pieces of data to solve the problem. Merge sort works in the following way:
38 |
39 | 1. Divide into 2 collections. Mergesort will take the middle index in the collection and split it into the left and right parts based on this middle index.
40 | 2. Resulting collections are again recursively splited and sorted
41 | 3. Once the sorting of the two collections is finished, the results are merged
42 | 4. Now Mergesort it picks the item which is smaller and inserts this item into the new collection.
43 | 5. Then selects the next elements and sorts the smaller element from both collections
44 |
45 | #### QUICKSORT
46 |
47 | Quick sort is better than merge sort from a memory usage comparison. Because quick sort doesn’t require additional storage to work. It only uses a small auxiliary stack.
48 |
49 | Merge sort is a sorting technique based on divide and conquer technique.
50 | With worst-case time
complexity being Ο(n log(n)). It divides input array in two halves, calls itself for
51 | the two halves and then merges the two sorted halves.
52 |
53 | 1. If the array contains only 1 element or 0 elements then the array is sorted.
54 | 2. If the array contains more than 1 element:
55 | 3. Select randomly an element from the array. This is the "pivot element".
56 | 4. Split into 2 arrays based on pivot element: smaller elements than pivot go to the first array, the ones above the pivot go into the second array
57 | 5. Sort both arrays by recursively applying the Quicksort algorithm.
58 | 6. Merge the arrays
59 |
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/SortingAlgorithm/SelectionSort.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace CodingPractice.SortingAlgorithm
6 | {
7 | ///
8 | /// Worst-case performance О(n2)
9 | /// Best-case performance О(n2)
10 | /// Average performance О(n2)
11 | /// Worst-case space complexity О(n) total, O(1) auxiliary
12 | /// The selection sort algorithm sorts an array by repeatedly finding the minimum element
13 | /// (considering ascending order) from unsorted part and putting it at the beginning.
14 | /// The algorithm maintains two subarrays in a given array.
15 | /// 1) The subarray which is already sorted.
16 | /// 2) Remaining subarray which is unsorted.
17 | /// https://en.wikipedia.org/wiki/Selection_sort
18 | ///
19 | public class SelectionSort
20 | {
21 | public static void selectionSort()
22 | {
23 | int[] array = { 2, 1, 4, 6, 2, 56, 9, 3 };
24 | int n = array.Length;
25 | int c = 0, position = 0, d = 0, swap = 0;
26 | for (c = 0; c < (n - 1); c++)
27 | {
28 | position = c;
29 |
30 | for (d = c + 1; d < n; d++)
31 | {
32 | if (array[position] > array[d])
33 | position = d;
34 | }
35 | if (position != c)
36 | {
37 | swap = array[c];
38 | array[c] = array[position];
39 | array[position] = swap;
40 | }
41 | }
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.deps.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeTarget": {
3 | "name": ".NETCoreApp,Version=v2.0",
4 | "signature": "da39a3ee5e6b4b0d3255bfef95601890afd80709"
5 | },
6 | "compilationOptions": {},
7 | "targets": {
8 | ".NETCoreApp,Version=v2.0": {
9 | "CodingPractice/1.0.0": {
10 | "runtime": {
11 | "CodingPractice.dll": {}
12 | }
13 | }
14 | }
15 | },
16 | "libraries": {
17 | "CodingPractice/1.0.0": {
18 | "type": "project",
19 | "serviceable": false,
20 | "sha512": ""
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.dll
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.pdb
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.runtimeconfig.dev.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "additionalProbingPaths": [
4 | "C:\\Users\\senth\\.dotnet\\store\\|arch|\\|tfm|",
5 | "C:\\Users\\senth\\.nuget\\packages",
6 | "C:\\Program Files\\dotnet\\sdk\\NuGetFallbackFolder"
7 | ]
8 | }
9 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/bin/Debug/netcoreapp2.0/CodingPractice.runtimeconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "runtimeOptions": {
3 | "tfm": "netcoreapp2.0",
4 | "framework": {
5 | "name": "Microsoft.NETCore.App",
6 | "version": "2.0.0"
7 | }
8 | }
9 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/CodingPractice.csproj.nuget.cache:
--------------------------------------------------------------------------------
1 | {
2 | "version": 1,
3 | "dgSpecHash": "t89j6J1ofFJ50lksFmZTaxg6WCtOJQ7+BFox6WCv630hrYId45Hhgp/fSC0Ie4fDwyhLrJ43pdRwoeOoXFSd1g==",
4 | "success": true
5 | }
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/CodingPractice.csproj.nuget.g.props:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | True
5 | NuGet
6 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\project.assets.json
7 | $(UserProfile)\.nuget\packages\
8 | C:\Users\senth\.nuget\packages\;C:\Program Files\dotnet\sdk\NuGetFallbackFolder
9 | PackageReference
10 | 4.5.0
11 |
12 |
13 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/CodingPractice.csproj.nuget.g.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildAllProjects);$(MSBuildThisFileFullPath)
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.42000
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | using System;
12 | using System.Reflection;
13 |
14 | [assembly: System.Reflection.AssemblyCompanyAttribute("CodingPractice")]
15 | [assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
16 | [assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
17 | [assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
18 | [assembly: System.Reflection.AssemblyProductAttribute("CodingPractice")]
19 | [assembly: System.Reflection.AssemblyTitleAttribute("CodingPractice")]
20 | [assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
21 |
22 | // Generated by the MSBuild WriteCodeFragment class.
23 |
24 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.AssemblyInfoInputs.cache:
--------------------------------------------------------------------------------
1 | 9987c594e9edfd896c261ce4c066cb72cd5d9e68
2 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.csproj.CoreCompileInputs.cache:
--------------------------------------------------------------------------------
1 | f3a6064f86e86f391ae57f1c1f56960bfdffc26f
2 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.csproj.FileListAbsolute.txt:
--------------------------------------------------------------------------------
1 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.deps.json
2 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.runtimeconfig.json
3 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.runtimeconfig.dev.json
4 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.dll
5 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.pdb
6 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.csprojResolveAssemblyReference.cache
7 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.csproj.CoreCompileInputs.cache
8 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.AssemblyInfoInputs.cache
9 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.AssemblyInfo.cs
10 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.dll
11 | C:\Senthil\Interview_Sample\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.pdb
12 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.csprojResolveAssemblyReference.cache
13 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.csproj.CoreCompileInputs.cache
14 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.AssemblyInfoInputs.cache
15 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.AssemblyInfo.cs
16 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.dll
17 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\obj\Debug\netcoreapp2.0\CodingPractice.pdb
18 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.deps.json
19 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.runtimeconfig.json
20 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.runtimeconfig.dev.json
21 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.dll
22 | C:\Senthil\Interview_Sample\coding_interview\CodingPractice\CodingPractice\bin\Debug\netcoreapp2.0\CodingPractice.pdb
23 |
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.csprojResolveAssemblyReference.cache:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.csprojResolveAssemblyReference.cache
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.dll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.dll
--------------------------------------------------------------------------------
/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.pdb:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/CodingPractice/CodingPractice/obj/Debug/netcoreapp2.0/CodingPractice.pdb
--------------------------------------------------------------------------------
/Images/Binary_tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/Binary_tree.png
--------------------------------------------------------------------------------
/Images/Circularly-linked-list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/Circularly-linked-list.png
--------------------------------------------------------------------------------
/Images/Doubly-linked-list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/Doubly-linked-list.png
--------------------------------------------------------------------------------
/Images/Justsearch.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/Justsearch.gif
--------------------------------------------------------------------------------
/Images/SQLNOSQL.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/SQLNOSQL.png
--------------------------------------------------------------------------------
/Images/Singly-linked-list.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/Singly-linked-list.png
--------------------------------------------------------------------------------
/Images/Test:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Images/adj.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/adj.jpg
--------------------------------------------------------------------------------
/Images/binary_search_tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/binary_search_tree.png
--------------------------------------------------------------------------------
/Images/bt_types.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/bt_types.JPG
--------------------------------------------------------------------------------
/Images/digraph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/digraph.png
--------------------------------------------------------------------------------
/Images/directed.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/directed.jpg
--------------------------------------------------------------------------------
/Images/doubly-linked-list2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/doubly-linked-list2.png
--------------------------------------------------------------------------------
/Images/graph-ex1.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/graph-ex1.jpeg
--------------------------------------------------------------------------------
/Images/graph.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/graph.JPG
--------------------------------------------------------------------------------
/Images/graphtypes.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/graphtypes.png
--------------------------------------------------------------------------------
/Images/imp-tree.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/imp-tree.jpg
--------------------------------------------------------------------------------
/Images/linkedlist.JPG:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/linkedlist.JPG
--------------------------------------------------------------------------------
/Images/mat.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/mat.jpg
--------------------------------------------------------------------------------
/Images/minheap.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/minheap.jpg
--------------------------------------------------------------------------------
/Images/osi-model.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/osi-model.png
--------------------------------------------------------------------------------
/Images/tree-parts.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/tree-parts.jpg
--------------------------------------------------------------------------------
/Images/tree_traversal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/tree_traversal.png
--------------------------------------------------------------------------------
/Images/trees.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/trees.jpg
--------------------------------------------------------------------------------
/Images/trie.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/trie.jpeg
--------------------------------------------------------------------------------
/Images/undirected.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/undirected.jpg
--------------------------------------------------------------------------------
/Images/weighted.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/senthil338/Software-Engineer-Interview/77fd0fa11150b5eaa93701faf344521900063593/Images/weighted.jpg
--------------------------------------------------------------------------------