...

Top 20 programming questions

Top 20 programming questions

Top 20 programming questions

1. Reverse a String without Slicing Operator or Inbuilt Function

Approach:

  • The string is converted into a list of characters. Use a loop, which starts from the beginning and end of this list of characters, and swap those characters moving towards the center. Finally this list is converted back into a string.

2. Swap Two Variables With and Without Using Third Variable

Approach:

  • Store the value of the first variable in a temporary variable.
    Assign the value of the second variable to the first variable. Assign the value of the temporary variable to the second variable.
  • Without Third Variable:
    Either arithmetic operations addition and subtraction or bitwise XOR may be used to swap the values.

3. Approach to Check if a Vowel is Present in a String

Approach:

  • Iterate through each character of the string. Check whether the character is a vowel (a, e, i, o, u, A, E, I, O, U).

4. Print Fibonacci Series Using Recursion

Approach:

  • Define a recursive function returning the nth Fibonacci number. Base cases for this function are the 0th and 1st Fibonacci numbers.
  • The recursive case is the sum of the (n-1)th and the (n-2)th Fibonacci numbers.

5. Check If a String Is a Palindrome

Approach:

  • Compare it with its reverse. If they are the same, the string is a palindrome.

6. Check if an Integer is a Palindrome

Approach:

  • Convert it to a string.
  • Check whether the string is a palindrome using the string palindrome approach.
  • Or else, use while loop with modulus operator to to get each digit and reverse it and remaining process is same.

7. Array Sorting in Ascending and Descending Order

Approach:

  • The sorting algorithms for the array would be bubble sort, selection sort, or quicksort.
  • In ascending order, arrange the elements in such a a way that the smallest element comes first followed by the next smallest element till the largest element.
  • In descending order, arrange the elements in such a way that the largest element comes first followed by the next largest element till the smallest element.

8. Factorial of a number

Approach:

  • The approach would be to define a function that calculates the factorial, either by loop iteration or recursively. The numbers from 1 through N would be multiplied against each other.

9. Remove All Duplicates from a String

Approach:

  • Keep track of the characters that already appeared in a set.
  • Traverse a string and build a new one with unique characters.

10. Check Whether the Given Number is Prime

Approach:

  • The number is less than 2; hence, it’s not a prime.
  • Check for factors from 2 up to the square root of the number.
  • If no factors are found, then it’s a prime number.

11. Reverse the Number

Approach:

  • Turn the number into a string.
  • Reverse the string and then again convert it into a number.

12. Count Number of Digits in an Integer

Approach:

  • Firstly, convert the number into a string and then count the characters.
  • Secondly, Use a loop to count the number of division by 10.

13. Remove All Duplicates from a List

Approach:

  • Using a set, record the unique element.
  • Iterating through the list to build another list with unique elements.

14. Check Whether the Given Number is Armstrong

Approach:

  • Calculate the number of digits in the number.
  • Raise each digit to the power of the number of digits and sum them.
  • If the sum equals the original number, it is an Armstrong number.

15. Find the Sum of Squares of Digits of a Number

Approach:

  • Convert the number into a string to traverse each digit.
  • Take each digit back as an integer, square it, and finally sum the squares.

16. Reverse Each Word in a Given String

Approach:

  • Split the string into words.
  • Reverse each word individually.
  • Join the reversed words back into a string.

17. Star Pattern Problems

Approach:

  • Left Aligned: A problem that requires one to print stars in nested loops, with every line containing an increasing number of stars.
  • Right Aligned: First print spaces and then stars.
  • Pyramid: Spaces and stars have to be printed in such a pattern that it forms a pyramid.

18. Find Number of Occurrences of a Character in a String

Approach:

  • Use a loop to traverse the string and count the occurrence of the character.

19. Whether the String is an Anagram

Approach:

  • The approach is, sort both strings and compare them.
  • Alternatively count the frequency of each character in both strings and compare counts.

20. Whether the String is a Pangram

Approach:

  • The approach is to make sure all the letters of the alphabet are included in the string at least once.
  • Observe the occurrence of every letter in a set and check whether it contains 26 letters or not.

If you don’t have an idea on problems, surf the internet learn them and practice them

Leave a comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.