MobiCastle Interview Question for Software Engineer in Tests

• 1
of 1 vote

Country: United Kingdom

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

Answer is simply n*(n²+1) / 2

``````long long rowSum(int n){
return n*(n*n+1)/2;
}
int main(){

int n;
cin>>n;

cout<<rowSum(n);

return 0;
}``````

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

#include<iostream>
using namespace std;

int main()
{
int no_of_rows, row_counter, pyramid_element = 1, column_counter,row_to_sum, row_sum = 0, before_numbers=0;
cout<<"How many rows of Pyramid you want?";
cin>>no_of_rows;

// Pyramid Construction
for(row_counter = 1; row_counter <= no_of_rows; row_counter++)
{
for(column_counter = 1; column_counter <= row_counter; column_counter++)
{
cout<<pyramid_element<<"\t";
pyramid_element++;
}
cout<<endl;
}

//Sum of row element of a pyramid
cout<<"Which row you want to sum up?";
cin>>row_to_sum;
for(row_counter = (row_to_sum - 1); row_counter >= 1; row_counter--)
{
before_numbers = before_numbers + row_counter;
}
for(row_counter = before_numbers + 1; row_counter <= before_numbers + row_to_sum; row_counter++)
{
row_sum = row_sum + row_counter;
}
cout<<"Sum of "<<row_to_sum<<"is : "<<row_sum;
return 0;
}

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

We should find out the first element of the row in order to calculate the sum of complete row. Using recursion/iteration we find out the first element of row in O(N) and using sum of integers formula (n*(n+1))/2 we find out the resultant sum in O(1).

``````import java.io.BufferedReader;
import java.io.IOException;

public class PyramidRowSum {
public static void main(String args[]) throws IOException {
long rowStartNum = rowStartNum(N, true);
long prevNumTotal = sumOfN(rowStartNum - 1);
long nextNumTotal = sumOfN(rowStartNum + (N - 1));
System.out.println(nextNumTotal - prevNumTotal);
}

public static long rowStartNum(long N, boolean recursion) {
if (recursion) {
if (N == 1) {
return 1l;
} else {
return rowStartNum(N-1, recursion) + N - 1;
}
} else {
long prevFirstElement = 1;
for (long i = 2; i <= N; i++) {
prevFirstElement = prevFirstElement + (i - 1);
}
return prevFirstElement;
}
}

public static long sumOfN(long N) {
return (N * (N + 1)) / 2l;
}
}``````

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

This problem is essentially summing a subarray/part of an array of nums from 1..len(nums). To do this, the only tricky part is to find out the starting index to sum and the how many elements to do so. Luckily, we can easily do this using the triangular numbers formula and we are also given an n. This leads to a simple solution. I present my 4-line solution in Python below.

Elegant Solution (Python):

``````def getNthRowSum(n):
n = n - 1 # Since we offset by 0
if n == 0:
return 1 # First row
starting_ind = (n*(n+1)//2)+1 # Adding 1 since arrays/lists start at 0
return sum(range(starting_ind, starting_ind+n+1))``````

Test code:

``````for i in range(1, 7):
print('%dth row: Sum = %d' % (i, getNthRowSum(i)))
'''
Output:
1th row: Sum = 1
2th row: Sum = 5
3th row: Sum = 15
4th row: Sum = 34
5th row: Sum = 65
6th row: Sum = 111
'''``````

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

Let's denote S(N) = row1 + row2 + .... + rowN. Then F(N) = S(N) - S(N-1), when F is the function we have to compute.
On the other hand, S(N) = 1 + 2 + ... + N = N (N+1) / 2 -- it's just aritmetic series.

So the answer could be computed in O(1) time and memory:
de

``````def S(N):
return N*(N+1)/2

def getPramidRowSum(N):
if N == 1:
return 1
return S(N) - S(N-1)``````

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

Give each row an index, and we see that there's a recursion hiding here
0: 1
1: 2=1+1 3=1+2
2: 4=1+3 5=1+4 6
3: 7 8 9 10
4: 11 12 13 14 15
...

The sum uses N integers, starting from the first element of row N, which comes out to be sum of all integers from 1 to N-1, plus 1, which is N*(N-1)/2 + 1.

Ex:
Row 4
Start element = 4*(4-1)/2 + 1 = 6 + 1
7 + 8 + 9 + 10 = 1+6 + 1+7 + 1+8 + 1+9 = (1+1+1+1) + 4*6 + (1+2+3) [Notice the sum from 1 to N-1 comes up again)

so,
temp = N*(N-1) / 2
sum = N + (N + 1) * temp

``````def Sum(N):
temp = (N * (N-1)) / 2
return N + (N+1) * temp``````

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

What if the given number starts with an arbitrary number, not 1? The above solutions do not work.

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

static void print(int n){
int sum = (n-1) * (n) / 2;
int k = 1;
while( k <= n ){
System.out.println(sum + k);
k++;
}
}

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

python code

``````def get_sum(row):
return int((row**2+1)*row/2)``````

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

let n = 5
var last = 1
for i in 1..<n {
last += i
if i < n-1 {
continue
}
var sum = last
for j in 1..<n {
sum += last + j
}
print(sum)
}

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

``````let n = 5
var last = 1
for i in 1..<n {
last += i
if i < n-1 {
continue
}
var sum = last
for j in 1..<n {
sum += last + j
}
print(sum)
}``````

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

``````public static int countrownums(int rowno)
{
if(rowno==1)
return 1;
int sum=0;
int val=((rowno*(rowno-1))/2)+1;
for(int i=1;i<=rowno;i++)
{
sum+=val++;
}
return sum;
}``````

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

``````public static int countrownums(int rowno)
{
if(rowno==1)
return 1;
int sum=0;
int val=((rowno*(rowno-1))/2)+1;
for(int i=1;i<=rowno;i++)
{
sum+=val++;
}
return sum;
}``````

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

public class Sumofrowstriangle{

public static void main(String args[])
{
int i,j,k=0, n=5;
int result=0;
for(i=1;i<=n;i++)

{
for(j=1;j<=i;j++)
{
k++;
if(i==n)

{
result+=k;
System.out.print(k + " ");
}
}
}
System.out.println();
System.out.println(result);
}
}

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

public class Sumofrowstriangle{

public static void main(String args[])
{
int i,j,k=0, n=5;
int result=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
k++;
if(i==n)
{
result+=k;
System.out.print(k + " ");
}
}
}
System.out.println();
System.out.println(result);
}
}

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

public static void main(String[] args){

int rowNum = 3;

int firstElement = rowNum*(rowNum-1)/2 + 1;

System.out.println(firstElement);

int sum1 = (firstElement+ rowNum)* (firstElement+ rowNum-1)/ 2;

int sum2 = (firstElement-1)*(firstElement)/2;

int total = sum1-sum2;

System.out.println(total);

}

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.

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.

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.

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.