Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

Complexity: O(n)

package com.programs;

/*
 * Print list ['a','b','c','d',...'j'] in 3 columns
 * output:
 * a e i
 * b f j
 * c g
 * d h
 * 
 */
public class printPattern {

	public static void main(String args[])
	{
		char[] elements = {'a','b','c','d','e','f','g','h','i','j','k','l','m'};
		int num_of_elements = elements.length;
		int remainder = num_of_elements%3;
		int rows = (remainder > 0) ? ((num_of_elements/3) +1) : (num_of_elements/3);
		int modulo = rows * 3;
		int index = 0;
		
		for(int j=0;j<modulo;j++)
		{
			if(index<num_of_elements)
				System.out.print(elements[index]+" ");
			index+=rows;
			if((index)<modulo)
				continue;
			else
			{
				index = ((index+1)%modulo);
				System.out.println("");
			}
		}
	}
}

- cobra July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

O(n) complexity.

#include <stdio.h>

void printPattern (char* str, int len) {
  int row = (len%3 == 0) ? (len/3) : ((len/3)+1) ;
  int i ;
  for ( i = 0 ; i < row ; i ++ ) {
    printf ( "%c", str[i] ) ;
    printf ( " %c", str[i+row] ) ;
    if ( (i+(row<<1)) < len )
      printf ( " %c", str[i+(row<<1)] ) ;
    printf ("\n") ;
  }
}

int main () {
  char str[] = {'a','b', 'c', 'd', 'e', 'f' , 'g', 'h', 'i', 'j'} ;
  printPattern (str, sizeof(str)/sizeof(str[0])) ;
  return 0;
}

- Psycho July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

here is the solution with o(n^2) since it involves rows and columns, I am not sure if it can be done with less complexity.

#include <stdio.h>

/*
 * Print List as columns
 */

void main()
{
    char vals[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
    int length = sizeof(vals)/sizeof(char);
    int rows = length/3;
    int remainder = length%3;
    if (remainder > 0)
    {
        rows++;
    }

    int i=0, j;
    for (i=0; i<rows  ; i++)
    {
        for (j=i ; j<=length ; j+=rows)
        {
            printf("%c", vals[j]);
        }
        printf("\n");
    }
}

- ethioer July 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintListInColumns {

	
	public static void main(String[] args) {
		
		char arr[] ={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
		char brr[][]= new char[4][3];
		int k=0;
		
		for(int j=0;j<3;j++)
		{
			for(int i=0;i<4;i++)
			{
				if(i>=2&&j>1)
				{
					brr[i][j]=' ';
				}
				else
				{
				brr[i][j]=arr[k];
				k++;
				}
			}
		}
		
		for(int j=0;j<brr.length;j++)
		{
			for(int i=0;i<brr[j].length;i++)
			{
				System.out.print(brr[j][i]+" ");
			}
			System.out.println(" ");
		}
	}
}

- codingAddicted July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintByColumn {


	public static void print(int[] input, int row){
		String[] s = new String[row];


		for(int i=0; i<input.length;i++){
			if(s[i%row]==null){
				s[i%row]=input[i]+" ";
			}else{


				s[i%row] +=input[i]+" ";
			}
		}

		for(String str:s){
			System.out.println(str);
		}

	}

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

}

- Anonymous July 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Character[] arr = {"A", "B", "C", "B"}
for (int i = 0; i < arr.length; i++){
if (i & 3==0){
system.out.print(arr[i]);
}
}

- Anonymous March 16, 2017 | 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