Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

Write a Java program to print Pascal’s Triangle Pattern.

Answer»

public class Main { public static void printPascalsTriangle(int n){ for (int i = 0; i < n; i++) { int number = 1; System.out.printf("%" + (n - i) * 2 + "s", ""); for (int j = 0; j <= i; j++) { System.out.printf("%4d", number); number = number * (i - j) / (j + 1); } System.out.println(); } } public static void main(String[] args) { int n = 8; printPascalsTriangle(n); }}

Output:

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1Conclusion

Design Patterns ensure that the reusable solutions that are well-tested are implemented which improves the code flexibility and paves way for DEVELOPING smart and EXTENDIBLE solutions that solve problems easily. Due to this, the demand for software developers to KNOW the best design practices and implementation has been rising steadily. These are generally discussed in the LLD (Low-Level Design) round of the company interviews. In this article, we have covered the most commonly asked design patterns interview QUESTIONS for both freshers and experienced software professionals.

Useful Resources:

  • System Design
  • Practice
2.

Write a Java program that takes a String as an input and prints the pattern in the increasing order of odd numbers as shown in the example below:

Answer»

If String input is PATTERN, then the pattern will be: P (First Character Once) AAA (SECOND Character thrice) TTTTT (Third Character 5 times) TTTTTTT : EEEEEEEEE : RRRRRRRRRRR :NNNNNNNNNNNNN :

Solution:

import java.util.Scanner;public class InterviewBitStringPattern{ public static void printStringPattern(String input){ for (int i = 1; i <= input.length(); i++) { for (int j = input.length(); j > i; j--){ System.out.print(" "); } for (int K = i * 2 - 1; k >= 1; k--){ System.out.print(input.charAt(i - 1)); } System.out.println(""); } } public static void main(String[] args) { // to CREATE a new Scanner object Scanner scanner = new Scanner(System.in); // to get the String from the user System.out.println("Enter Input String: "); String input = scanner.nextLine(); System.out.println("PRINTING Pattern....."); printStringPattern(input); }}

Output:

Enter Input String: PATTERNPrinting Pattern..... P AAA TTTTT TTTTTTT EEEEEEEEERRRRRRRRRRRNNNNNNNNNNNNN
3.

Write a Java program to print Diamond Number Pattern.

Answer»

public class InterviewBitDiamondNumber{ public STATIC void diamondNumberDisplay(int maxNum){ for (int i = 1; i &LT;= maxNum; i++){ int n = maxNum; for (int J = 1; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--) { System.out.print(k); } for (int l = 2; l <= i; l++) { System.out.print(l); } System.out.println(); } for (int i = maxNum-1; i >= 1; i--){ int n = maxNum-1; for (int j = 0; j<= n - i; j++) { System.out.print(" "); } for (int k = i; k >= 1; k--){ System.out.print(k); } for (int l = 2; l <= i; l++){ System.out.print(l); } System.out.println(); } } public static void main(String[] args) { int n = 5; diamondNumberDisplay(n); }}

OUTPUT:

1 212 3212343212345432123454321234 32123 212 1
4.

Write a Java Program to display the left triangle star pattern on the system console.

Answer»

This can be again be achieved by using nested loops and calculatingly adding spaces and stars as shown in the logic below:

public class InterviewBitLeftPyramid{ public static void printLeftTriangleStars(INT n) { int j; for(int i=0; i<n; i++){ // OUTER loop for number of rows(n) for(j=2*(n-i); j>=0; j--){ // for spaces System.out.print(" "); // to print space } for(j=0; j<=i; j++){ // for columns System.out.print("* "); // Print star and give space } System.out.println(); // Go to next line after EVERY row } } public static void main(String args[]){ printLeftTriangleStars(5); //print stars of 5 rows in left TRIANGLE fashion }}

OUTPUT:

* * * * * * * * * * * * * * *
5.

Write a Java Program to display the pyramid as per the below design.

Answer»

* * * * * * * * * * * * * * *

This can be achieved by using nested loops and calculatingly adding spaces and STARS as shown in the logic below:

public class InterviewBitPyramid{ public STATIC void printPyramid(int N) { for (int i=0; i<n; i++){ // for number of rows  for (int J=n-i; j>1; j--) {  System.out.print(" "); //print space } //for number of columns for (int j=0; j<=i; j++ ) {  System.out.print("* "); // print star }  //end-line after every row System.out.println(); }  }  public static void main(String args[]){  printPyramid(5); //Print PYRAMID stars of 5 rows }}

Output:

* * * * * * * * * * * * * * *