Interview Question for Java Developers


Country: India




Comment hidden because of low score. Click to expand.
1
of 1 vote

The following code also works for non-square matrix

public void printMatrixAntiDiag(int[][] matrix) {
	if (matrix.length > 0 && matrix[0].length > 0) {
		int m = matrix.length, n = matrix[0].length;
		int layer = m + n - 1;
		for (int i = 0; i < layer; i++) {
			int row = Math.max(i-(n-1), 0);
			int col = Math.min(i, n-1);
			while (row < m && col >=0) {
				System.out.print(matrix[row++][col--] + " ");
			}
		}
	}
}

- chriscow January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Very simple solution and works with unsorted array and arrays with different size.
Ah.. generic too. Could be applied for chars, strings and so on...

/*
  i have an array{{1,2,3},{4,5,6},{7,8,9}}.I want output=1,2,4,3,5,7,6,8,9  
 */

#include <iostream>
#include <vector>

template <typename T>
void print_specific_order(std::vector< std::vector<T> > & vec) {
	T saved;
	bool is_saved = false;
	for(auto v: vec) {
		for(int i = 0; i < v.size(); ++i) {
			if(i == v.size()-1) {
				saved = v[i];
				is_saved = true;
				continue;
			}

			std::cout << v[i] << std::endl;

			if(is_saved) {
				std::cout << saved << std::endl;
				is_saved = !is_saved;
			}
		}
	}

	std::cout << saved << std::endl;
}

int main() {
	std::vector< std::vector<int> > vec {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
	print_specific_order<int>(vec);
	return 0;
}

- Felipe Cerqueira January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void printElements(int[][] input) {
		for (int i = 0; i < input.length; i++) {

			int myNewArr[] = input[i];
			for (int j = 0; j < myNewArr.length; j++) {
				System.out.print(myNewArr[j]);
			}

		}
	}

This is working for the above given input.

- Ashish January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No it doesn't. It prints out 1, 2, 3, 4, 5, 6, 7, 8, 9
Correct me if I'm wrong.

- chriscow January 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ahh My bad, didnt see look @ the output correctly.

- Ashish January 29, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		//int[][] a = {{1,2,3}, {4,5,6}, {7,8,9}};
		//int[][] a = {{1,2}, {3,4}};
		int[][] a = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}};
		
		int x = a[0].length;
		
		for(int i = 0; i < x; i++){

			System.out.println(a[0][i]);
			printIndex(a, i-1 , 1);
		}
		
		for(int i = 1; i < a.length; i++){
			System.out.println(a[i][x-1]);
			printIndex(a, x-2 , i + 1);
		}
		

	}
	
	public static void printIndex(int[][] a, int x, int y){
		if(x < 0 || y >= a.length)
			return;
		
		System.out.println(a[y][x]);
		
		printIndex(a, x -1, y +1);
	}

- Anonymous January 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
using namespace std;

#define MAX 5

int main(){
int a[MAX][MAX] =
{{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9,10},
{11,12,13,14,15},
{16,17,18,19,20},
{21,22,23,24,25}};
int i=0,j=0;
while(i<MAX){
cout << a[i][j] << " -> ";
if(i==MAX-1){
i = j+1; j = MAX-1;
}
else if(j==0){
j = i+1; i = 0;
}
else{
i++; j--;
}
}
cout << endl;

return 0;
}

- kuldeep January 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Simple1 {
public static void main(String[] args){
int [] arr[]={{1,2,3},{4,5,6},{7,8,9}};
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr.length;j++){
System.out.print(arr[i][j]+",\t");
}

}
}
}

- Sana February 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

works with square array

void prettyPrint(int[][] arr) {

        for (int i = 0; i < arr.length; i++) {
            int k = 0;
            for (int j = i; j >= 0; j--) {
                System.out.print(arr[k++][j] + " ");
            }
        }

        for (int i = 1; i < arr.length; i++) {
            int k = i;
            for (int j = arr.length-1; j >= i; j--) {
                System.out.print(arr[k++][j] + " ");
            }
        }
    }

- Neha February 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class QueArray {
public static void main(String[] args){
int nums[][] = {{1,2,3},
{4,5,6},
{7,8,9}};

for(int x[]: nums){
for(int y: x){
System.out.print(y + " ");
}
}
System.out.println();
}
}

- Utsav Parashar April 10, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More