Interview Question


Country: India
Interview Type: Written Test




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

#include<conio.h>
#include<stdio.h>

main()
{
 int a[5]={2,4,1,5,3};
 int b[5]={0,0,0,0,0};
 int i,p,q;
 for (i=0;i<5;i++)
 {
  b[i]=b[i-1]+a[i];   
 }
printf("Enter i : ");
scanf("%d",&p);
printf("\nEnter j : ");
scanf("%d",&q);
printf("\nsum of terms between i & j is : %d  ",b[q]-b[p-1]);
 getch();     
}

//Can we do better??

- mohit May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Idea is good, but your code sucks.

- Anonymous May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

//First put everything in a hasmap in O(n) time
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
int arr[] = {2,4,1,5,3};
int j=1;
int sum=0;

for(int i : arr)
{
sum=sum+i;
hm.put(j, sum);
j++;
}

Now when i=2 and j=5
just say hm.get(5) - hm.get(2);
you will get the answer in O(1)

- Spammer May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
        int A[] = {1,2,3,4,5,6,7};
        int i=1;
        int start, end;
        
        printf("\n please enter strt & end indexes \n");
        scanf("%d %d", &start, &end );
        if(!( (start >=0 && start <end) && (end < sizeof(A) ) ))
        {
                printf("\n entered incorrect indexes \n");
                return 0;
        }
        while ( i< sizeof(A) )
        {
                A[i] += A[i-1] ;
        }
        if( start > 0 )
                printf("\n sum from %d to %d is = %d \n", start, end, A[end]-A[start-1] );
        else
                printf("\n sum from %d to %d is = %d \n", start, end, A[end] );
        return 0;
}

- siva.sai.2020 May 06, 2012 | 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