GeekInterview.com
Series: Subject: Topic:

Algorithm Questions

Showing Questions 1 - 20 of 51 Questions
First | Prev | | Next | Last Page
Sort by: 
 | 

Python - how to write a function

Asked By: Jeffsdf | Asked On: Apr 6th, 2013

How to write a function kinda_format ( ) that take three arguments (each of which positive int ) - a,b,c- and tests whether the equality a^n +b^n=c^n holds for each n{2,3,...,10}. The function should return false if the equality does not hold for any value of n in the given range, and if there is a value...

Sorted matrix

Asked By: hari_nowayout | Asked On: Aug 27th, 2008

You are given a matrix. The matrix elements are such that they are sorted both horizonTALLY and vertically. Given an element of the matrix, your job is to obtain its position in an efficient manner.Example: 10 20 30 15 35 98 48 76 110input: 76output: 3rd row, 2nd column.

Answered by: sandy on: Mar 24th, 2013

I assume you can do it in the following way: "python class Coordinate: def __init__(self,x=0,y=0): self.x = x self.y = y def findElement(a,n): if a == Non...

Answered by: frogInSea on: Mar 14th, 2013

I think it can be solved in a two dimensional Binary-Search way

Duplicate elements array

Asked By: hari_nowayout | Asked On: Aug 27th, 2008

You have an array of size '2n' of these, 'n+1' elements are distinct and 1 element is repeated 'n' times. You must find the repeated element and say how many times it has repeated. Your solution must use minimum no. Of comparisons. Also, say how many comparisons your solution will make in the worst case?

Answered by: shankar on: Oct 16th, 2012

Since an element is repeated more than half the occurance of the array, for every two didtinct elements discard them from the array. At the end of the loop, the most repeated element will be left.

This is O(n)

Answered by: nsudheer on: Jun 4th, 2011

In 2n elements, one element is repeated n times.Let us assume that the repeated number is 4. The elements in array can be as given below (there can be other combinations).1 4 2 4 3 4 5 4 6 4 ...In all...

Calculate primary and secondary diagonal

Asked By: wiseboy | Asked On: Jan 26th, 2009

Write an algorithm that calculates all the elements of rows and columns of a square matrix and calculate the total of primary and secondary diagonal.

Answered by: Ashima on: Aug 9th, 2012

Assuming the matrix size is row, col and i represents the row and j represents the col. Get the primary Diagonal where i==j and secondary diagonal where i==j-1. Below is the code for that.{geshibot la...

Answered by: Asher The King on: Jul 10th, 2011

int sum=0;For (int i=0;i<Line;i++)    For(int j=0;j<COL;j++)    {         //Primary diagonal     &n...

A car rental firm leases its cars for $250 per day.

Asked By: jovan | Asked On: Mar 31st, 2010

A car rental firm leases its cars for $250 per day. The manager gives a discount based on the number of days that the car is rented. If the rental period is greater than or equal to 7 days then a 25% discount is given. Read the period and print the discount given

Answered by: Michael Loofburrow on: Jun 12th, 2012

Here it is in C++

Code
  1. void Discount()
  2. {
  3.         int days;
  4.         float discount;
  5.  
  6.         cout >> "How many days has the car been rented?" << endl;
  7.         cin << days;
  8.        
  9.         if(days >= 7)
  10.                 discount = (250*days)*0.25;
  11.                
  12.         cout >> "The discount is " >> discount >> " dollars."
  13.        
  14.         return;
  15. }

Answered by: midosouf on: Jan 21st, 2011

     Sub rent()      Dim x, i, y As Single    i = InputBox("nb of day")       x = 250      If i >= 7 Then         y = x * i         i = y * 25 / 100      Â...

Algorithm to print count and possible string outcomes of a given input string

Asked By: taru22 | Asked On: Apr 27th, 2012

Write an efficient algorithm to print count and possible string outcomes of a given input string by retaining order and considering all characters during this process problem: given a input string as "0000" and following input scheme for the zeroes, print the possible outcomes by retaining order of...

Compute average of two scores

Asked By: The_Kerr1431 | Asked On: Jun 25th, 2010

Describe an algorithm to compute the average of two scores obtained by each of the 100 students

Answered by: Sandhya.Kishan on: Apr 18th, 2012

This can be done using structures

struct student
{
int score1;
int score2;
}st[5];

int sum,avg;
printf("Enter the first score:");
scanf{"%d",&st[i].score1);
printf("Enter the second score:");
scanf{"%d",&st[i].score2);
for(i=0;i<5;i++)
{
sum=st[i].score1+st[i].score2;
avg=sum/2;
printf("average=%d",&avg);
}

Read the heights in inches and wieght in pounds

Asked By: jovan | Asked On: Mar 31st, 2010

Read the heights in inches and wieght in pounds of an individual and compute and print their bmi=((weight/height)/height)*703

Answered by: Sandhya.Kishan on: Apr 17th, 2012

#include
#include
void main
{
int h,w,bmi;
clrscr();
printf{"Enter your height in inches:"};
scanf{"%d",&h};
printf{"Enter your weight in pounds:"}
scanf{"%d",&w};
bmi=((w/h)/h)*703;
printf{"BMI=%d",&bmi};
getch();
}

Book index rare order

Asked By: pn8572 | Asked On: Mar 30th, 2012

A book collector has a special requirement. He has a book which contains a letter with english alphabets. But the sequence of letters is not the standard english. I.E. The alphabet sequence are not a,b,c.....Z. he has to find the letter sequence from the index for the book. What could be the right algorithm...

There are numbers from 1 to n in an array. Out of these, one of the number gets duplicated and one is missing. The task is to find out the duplicate number. Conditions: you have to do it in o(n) time without...

Asked By: vipin gupta | Asked On: Dec 13th, 2006

Star Read Best Answer

Editorial / Best Answer

Answered by: David Rachutin

Answered On : Dec 27th, 2006

use the following method:

mark the missing number as M and the duplicated as D

1) compute the sum of regular list of numbers from 1 to N call it RegularSum

2) compute the sum of your array (the one with M and D) call it MySum

now you know that MySum-M+D=RegularSum

this is one equation.

the second one uses multiplication:

3) compute the multiplication of numbers of regular list of numbers from 1 to N call it RegularMultiplication

4) compute the multiplication of numbers of your list  (the one with M and D) call it MyMultiplication

now you know that MyMultiplication=RegularMultiplication*D/M

at this point you have two equations with two parameters, solve and rule!

Answered by: kaviya on: Mar 12th, 2012

try this with quick sort... i think it is the best among the sorting techniques....

Answered by: Abhijit Samantaray on: Mar 6th, 2012

Let sum of 1 to n be totalSum
Let sum of the numbers in array be arraySum
var diff=totalSum - arraySum
Repeated number=n - (diff)
Missing number= n+(diff)

Post order binary tree traverse

Asked By: Dheerendra_juli | Asked On: Jul 22nd, 2009

Design a conventional iterative algorithm to traverse a binary tree represented in two dimensional array in postorder.

Answered by: Sandhya.Kishan on: Mar 10th, 2012

In Postorder traversal sequence we first look for the left node then the right node and then the root. Algorithm:

Code
  1. void postOrder(tNode n)
  2. {
  3.  if(n==null)
  4.     return;
  5.     postOrder(n.left);
  6.    postOrder(n.right);
  7.     visit(n);
  8. }

Importance of algorithm

Asked By: Dheerendra_juli | Asked On: Jul 22nd, 2009

What is the importance of algorithms in the field of computer science?

Answered by: Sandhya.Kishan on: Mar 7th, 2012

Algorithms are blue prints of a program which gives all the details and functionality involved in finding the solution to a problem.It is important as we can build a program on any platform with the help of an algorithm.

Algorithm characteristics

Asked By: Dheerendra_juli | Asked On: Jul 22nd, 2009

List out the characteristics of an algorithm

Answered by: Sandhya.Kishan on: Mar 7th, 2012

1.It should be simple.
2.Generally written in simple language.
3.It involves finite number of steps.
4.should be executed in short period of time.
5.Output of algorithm should be unique.

Compute the sum of integers algorithm

Asked By: sally.ar_93 | Asked On: Oct 12th, 2011

Write an algorithm to compute the sum of integers from 1 to 50 ? Please write the answer in descriptive way to solve this ?

Answered by: kala725 on: Dec 11th, 2011

As sum from 1 to n === n*(n+1)/2

so sum from 1 to 50=== 50*51/2

Very simple

Write an algorithm to find the minimum of numbers where n is any arbitary natural number. N is given to you by the user as the first value.

Asked By: marzieh eskandari | Asked On: Oct 11th, 2007

Answered by: jjtharappel on: Nov 24th, 2011

The one I posted on Nov23 will not sork. A slight modification. Below is the working code "cobol ACCEPT WS-SYSIN-DATA FROM SYSIN IF WS-SYSIN-NUM IS NUMERIC C...

Answered by: jjtharappel on: Nov 23rd, 2011

I = N / 10
J = I * 10
K = N - J
min_num = K

If I > 10
Do while I < 10
N = I
I = N / 10
j = I * 10
K = N - J
if K < min_num
min_num = K
end-if
end-do

Prinf "Minimum number is " min_num
Return

What is the difference between instance(per object ) and static (shared by all objects) in report while declaring GLobal variable?

Asked By: Muthukumaran | Asked On: Nov 2nd, 2007

Answered by: nikhil on: Oct 12th, 2011

When Instance is created memory space is allocated and its unique for that instance. but for static object ONLY one copy is created for that so that one memory location for all.

Answered by: randeep singh on: Nov 21st, 2007

In case of instance per object every instance have its own memory space but in case of static variable every instance access same memory space for that variable.

Linked list

Asked By: suchana datta | Asked On: Aug 3rd, 2010

How will you reverse a singly linked list?

Answered by: Chirag on: Oct 11th, 2011

Here is the code"c #include typedef struct node mnode; mnode* reverse_ll(mnode* head) { if(head==NULL) return; mnode *temp1,*temp2,*temp3; temp2=head->next; head->next=N...

Answered by: nishkarshs on: Dec 30th, 2010

How to think: Make a 'temp' node similar to any other node. This has a 'next' element. So, all we need now is the 'prev' element. Hence, we create a pointer variable prev.Algo:...

Linked list kth largest element

Asked By: vmythilee | Asked On: Aug 31st, 2010

How to find the kth largest element in a linked list? Can you do this without using extra memory and also without modifying (sorting) the list?

Answered by: batman on: Oct 4th, 2011

Not enough information about the data in the linked list. Solution 1. Assuming int & no duplicates & rather small [min,max] interval: Iterate through the list once to find the min&max. Make a bi...

Answered by: Amit Jain on: Jun 9th, 2011

If the question assumes that the extra memory used should not be a function of x, then one possible solution can be the use of heap to make min-queue. After k elements have been entered , compare the ...

Recursion

Asked By: Dheerendra_juli | Asked On: Jul 22nd, 2009

What is recursion? Which data structure is used for recursion

Answered by: Jitendra Chittoda on: Sep 4th, 2011

Stack data structure is used to maintain the information about the recursive functions.

Answered by: kbjarnason on: Jul 2nd, 2010

Recursion occurs when a function, either directly or indirectly, calls itself.  An example might be a factorial function:unsigned fact(unsigned n){   /* 0! is by definition 1; */ &...

How do display the output as reverse of input, like input is "this is a hat" and the output should be "hat a is this"?

Asked By: monu2784 | Asked On: Jun 16th, 2006

Answered by: maneesh on: Aug 8th, 2011

Push the characters onto a stack and then pop every1. Or get the string length and then iterate backwards from the last character.

Answered by: guest on: Aug 5th, 2011

#This is why Perl Rocks!

Code
  1. #!/usr/bin/perl
  2.  
  3.  
  4. print "Enter String:
  5. ";
  6.   my $input;
  7.   chop($input = <STDIN>);
  8.  
  9.   my $revers = reverse $input;
  10.   print "$revers
  11. ";
  12.  

First | Prev | | Next | Last Page

 

 

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.