PRATIAN Interview Question for Java Developers


Country: India
Interview Type: Written Test




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

c++, implementation

#include <iostream>

using namespace std;

void printSeries(int n) {
    int i, s;

    s = 1;
    i = 0;
    while (i < n) {
        s += i * i;
        cout << s << " ";
        i++;
    }
}

int main(int argc, char* argv[]) {
    printSeries(10);

    return 0;
}

- kyduke December 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

c#, 2 variants.

using System;

namespace DisplaySeries {

    class Program {

        // Variant 1
        static private void DisplaySeriesV1( int n ) {
            for ( int i = 0; i <= n; i++ ) {
                var res = (i + 2) * (2 * i * i - i + 3) / 6;
                Console.Write($"{res} ");
            }
        }
        
        // Variant 2
        static private void DisplaySeriesV2( int n ) {
            var prev = 1;
            for (int i = 0; i <= n; i++) {
                var res = prev + i * i;
                prev = res;
                Console.Write($"{res} ");
            }
        }

        static void Main(string[] args)
        {
            DisplaySeriesV1(10);
            Console.WriteLine();
            DisplaySeriesV2(10);
            Console.ReadLine();
        }
    }
}

- zr.roman December 10, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Two examples. First time through I misread the prompt and thought they only wanted N, so I derived a summation formula based on looking up the i^2 one. I then realized it was asking for the whole series, so an alternative that uses an accumulator is listed as well. Both run in O(n) and use O(1) space, and the alternate might end up being a hair faster due to fewer mathematical operations.

int[] computeUpToNInSeries(int n) {
	final int[] series = new int[n];		
	for (int i = 1; i <= n; i++) {
		series[i - 1] = compute(i);
	}

	return series;
}

int compute(int i) {
	return (i * (i - 1) * (2*i - 1)) / 6 + 1;
}

int[] alternateComputeUpToNInSeries(int n) {
	final int[] series = new int[n];

	int accumulator = 1;
	for (int i = 1; i <= n; i++) {
		series[i - 1] = accumulator;
		accumulator += i * i;
	}

	return series;
}

- Mdhornet90 December 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

void displaySeries(int n)
{
	int sum =1;
	cout <<endl<<" series is :";
	for( i=1;i<n;i++)
	{
		cout << sum << ' ';
		sum = sum+(i*i);
	}

}

- sv December 11, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class A {

public static void main(String[] args) {
//1,2,6,15,31,56,.....
int n1 = 0,n2 = 1,n3;
for(int i=0;i<20;i++){
n3=n2+i*i;
n2=n3;



System.out.print(n3+",");
}

}




}

- MD TAUKIR ALAM December 16, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<math.h>
int main()
{
	int n;
	int i=1,j=1;
	printf("enter no of terms in series");
	scanf("%d",&n);
	printf("%d\t",i);
	while(j<=n)
	{
		i+=(pow(j,2));	
		j++;
		printf("%d\t",i);
	}
	return 0;
}

- apoorva December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include<stdio.h>
#include<math.h>
int main()
{
	int n;
	int i=1,j=1;
	printf("enter no of terms in series");
	scanf("%d",&n);
	printf("%d\t",i);
	while(j<=n)
	{
		i+=(pow(j,2));	
		j++;
		printf("%d\t",i);
	}
	return 0;
}

- apoorva December 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public static void main(String[] args) {
int n=10,n1=1;
for(int i=0;i<=n;i++){
n1=(int) (n1+Math.pow(i,2));

System.out.println(n1);
}

}
}

- Sanjeet Chaudhary December 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

//Ques=>Write the code to display the series 1 ,2,6,15,31,56....N

#include<stdio.h>

int main()
{
int nofterm,addprevious,i,value ;
addprevious=1;
printf("How many terms do u want to print");
scanf("%d",&nofterm);
for(i=0;i<nofterm;i++)
{
value=addprevious+i*i;
addprevious=value;

printf("%d->", value);
}
return 0;
}

- mithleshtechno January 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package javaapplication1;

public class JavaApplication1 {

public static void main(String[] args) {
int a=1,b=0;
for(int i=0;i<10;i++){
a=i*i+a;
System.out.print(" "+a);
}
}
}

- Lokesh February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package javaapplication1;

public class JavaApplication1 {

public static void main(String[] args) {
int a=1,b=0;
for(int i=0;i<10;i++){
a=i*i+a;
System.out.print(" "+a);
}
}
}

- Lokesh February 02, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[15],n;
scanf("%d",&n);
a[0]=1
for(i=1;i<=n;i++)
{
a[i]=a[i-1]+i*i;
}
for(i=0;i<=n;i++){
printf("%d",a[i]);
}
}

- Rohit Rumani March 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[15],n;
scanf("%d",&n);
a[0]=1
for(i=1;i<=n;i++)
{
a[i]=a[i-1]+i*i;
}
for(i=0;i<=n;i++){
printf("%d",a[i]);
}
}

- Rohit Rumani March 15, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[15],n;
scanf("%d",&n);
a[0]=1
for(i=1;i<=n;i++)
{
a[i]=a[i-1]+i*i;
}
for(i=0;i<=n;i++){
printf("%d",a[i]);
}
}

- Rohit Rumani March 15, 2016 | 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