We have discussed some very basic data structures. Sometimes, we need to decide which data structure to use at which place. Although is there is no hard and fast rule of using a data structure in a specific scenario, the below guidelines can help for general cases. For our program to be very efficient we must be aware of the sample data, our code needs to handle and a data structure should be used accordingly. Ordered Array :- If amount of data is small and predictable and search speed is …
-
-
Question: Write a program in C to print the given matrix in spiral order. Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Printing a matrix in spiral order can be better understood by the following image. Thus, printing a matrix in spiral order is just a way to traverse the matrix. The matrix can be supposed to be …
-
The general way to print any statement would be like:- But in this example we are using at least one semicolon(;). Our target is to print something on the screen without using even a single semi colon(;) . This is sort of a fun problem rather than an actual concept. It may seem to be impossible at once but we can utilize the fact that the statements inside an if condition is always executed and its result is used to determine the block to be executed. We can do something …
-
The term Space Complexity is misused for Auxiliary Space at many places. Following are the correct definitions of Auxiliary Space and Space Complexity. Auxiliary Space is the extra space or temporary space used by an algorithm. Space Complexity of an algorithm is total space taken by the algorithm with respect to the input size. Space complexity includes both Auxiliary space and space used by input. We can also say that the way in which the amount of storage space required by an algorithm varies with the size of the problem …
-
Question: Write a program in C to reverse a string using recursion Input: Hello Everyone Output: enoyrevE olleH We discussed the method to reverse the string using recursion in this post. But a recursive method is generally not preferred as it takes a longer execution time. This is also a classic interview question and we need to do it in place. We also need to be careful for the null character.
-
A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in sorted output as they appear in the input unsorted array. Some sorting algorithms are stable by nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like Heap Sort, Quick Sort, etc. However, any given sorting algorithm which is not stable can be modified to be stable. There can be sorting algorithm specific ways to make it stable, but in general, any comparison based sorting …
-
Question: Find the duplicate characters in a string? Input: floccinaucinihilipilification Output: a ,c ,f ,i ,l ,n ,o The idea for this problem would be:- Create a character array for the number of characters possible. Take 256 for all the ASCII characters as well. Traverse the array and print the characters that have a count greater than 1. Here is the source code for the same-
