niraj.nijju
BAN USERB.Tech CSE'12
http://codes-to-problem.blogspot.in/
- 1of 1 vote
AnswersImplement Cache in Java.
- niraj.nijju in United States for kindle
function should support
Object get(key)
void put(key, value)| Report Duplicate | Flag | PURGE
Amazon SDE1 - 0of 0 votes
AnswerFor a given number, print this number in words(in Indian style number system).
- niraj.nijju in India for looking for some good solution.
eg:
i/p 345 o/p: three hundred fourtyfive
i/p 3145 o/p: three thousand one hundred fourtyfive
i/p 314520 o/p: three lakh fourteen thousand five hundred twenty.
* http://en.wikipedia.org/wiki/South_Asian_numbering_system| Report Duplicate | Flag | PURGE
Algorithm - 0of 0 votes
Answersto reverse the content of stack,by only using stack operations.
- niraj.nijju in India
can it be better than n^2.| Report Duplicate | Flag | PURGE
- 0of 0 votes
AnswersFind a Pythagoras triplet from an array of integers
- niraj.nijju in India
better than O(n^2log n)| Report Duplicate | Flag | PURGE
check it
codes-to-problem.blogspot.in/2012/10/re-arrange-oddeven-to-oddeven-places.html
here it is working code:
codes-to-problem.blogspot.in/2012/09/sort-array-of-0-1-2-in-one-pass.html
Go with Circular Queue of size 1000.
if( !isFull(queue) )
when a new user comes fill it into queue,
else
pop a user from queue and save it to 1000thUser(variable)
inQueue the new user
1000thUser will answer. (check the no of user should cross 1000 otherwise output no. of user visited is < 1000)
use hashmap,
break url in 2 portion, one is the domain name part and other is rest one,
now apply hashfunction on both separately and concatenate them,(this will reduce chance of collision).
if the given url is already used then return the previously used hash value.
Working Code @
codes-to-problem.blogspot.in/2012/07/given-2d-matrix-which-has-only-1s-and.html
my be interviewer want b/t A to D
- niraj.nijju July 21, 2012it will be same stack
- niraj.nijju July 21, 2012reversestack(stack)
{
data=pop
reversestack(s);
reversepush(S,data)
}
recursivepush(s,data)
{
if(empty stack)
{push(s,data) return}
temp=pop
recursivepush(s,data)
push(s,temp)
}
No dear, we have to reverse the content not to generate a new stack
&
ONLY USING STACK OPRATION
if there is element a,b,c,d such that
a+b=d or a+b+c=d then u are done remove d from clone of A, first one is of n^2 and 2nd one is O(n).
.
.
what if Input : 1 2 4 8/9/10
A: 0 1 2 3
B: 0 2 3 4
C : 3 4 5
- so you fail
+1 good approach
- niraj.nijju July 20, 2012ok we can also find it with n^2 with hash table with O(n) space.
can even better solution to this n^2.
why ans is 2 for
int main() {
int x = 1;
x=x++/++x;
printf("%d\n",x);
return 0;
}
hey, thanks
I corrected it. Inform me if u find any other error.
a similar code here
codes-to-problem.blogspot.in/2012/07/wap-to-return-numbered-index-if-input.html
codes-to-problem.blogspot.in/2012/07/find-number-of-substrings-of-string.html
- niraj.nijju July 08, 2012codes-to-problem.blogspot.in/
codes-to-problem.blogspot.in/
codes-to-problem.blogspot.com/2012/07/verify-if-given-password-is-valid-or.html
codes-to-problem.blogspot.in/2012/07/find-seed-of-number.html
codes-to-problem.blogspot.in/2012/07/sort-array-containing-0s-and-1s-with.html
- niraj.nijju July 06, 20126 Race:
Race 1 : 1,2,3,4,5
Race 2 : 1,6,7,8,9
Race 3 : 1,10,11,12,13
Race 4 : 1,14,15,16,17
Race 5 : 1,18,19,20,21
Race 6 : 1,22,23,24,25
now we speed of all hores withe comparision to H1, and we can get the best 3 hores.
/*atoi is a function in the C programming language that converts a string into an integer numerical representation.
atoi stands for ASCII to integer. It is included in the C standard library header file stdlib.h. Its prototype is as follows:*/
// Working code
//INPUT : 24 83 0 2
//OUTPUT : 24 83 0 2
#include<stdio.h>
#include<string.h>
int atoi(int K[10],char string[30])
{
int j=0,i=0,temp=0;
while(string[i]!='\0')
{
temp=0;
while(string[i]!=' ' && string[i]!='\0')
temp=temp*10 + (string[i++]-'0') ;
if(string[i]==' ')
i++;
K[j++]=temp;
}
return j;
}
int main()
{
char string[30];
int i,a,A[10];
printf("\nEnter String : ");
gets(string);
a=atoi(A,string);
printf("\nIntegers are : ");
for(i=0;i<a;i++)
printf("%d ",A[i]);
printf("\n");
return 0;
}
try to get the answer of some random input byself on PAPER.
If you can then only try to design ur algo. my algo is simple a recursive call, try on paper then u will get my algo.
and tell if there is any wrong case
Niraj
/* The character 'a' to 'z' are encoded as 1 - 26. Given a string of digits,
compute the number of valid decodings of the string. For example,
both 'aa' and 'k' can be encoded as '11'. Hence num_valid_encodings('11') = 2. */
// WORKING CODE || recursive
#include <stdio.h>
#include <string.h>
int count_decode(char S[10],int n)
{
int x1=S[n-1]-'0',x2=S[n-2]-'0',z;
if(!n)
return 0;
else if(n==1)
{
return 1;
}
else
{
z=x2*10+x1;
// printf("z=%d ",z);
if(z>26)
return (count_decode(S,n-1));
else if(n==2)
return (count_decode(S,n-1) + count_decode(S,n-2)+1);
else
return (count_decode(S,n-1) + count_decode(S,n-2));
}
}
int main()
{
int i,j,n;
char S[10];
gets(S);
n=strlen(S);
printf("< %d >",count_decode(S,n));
return 0;
}
tell me if u find wrong answer
- niraj.nijju July 02, 2012/* Consider you have a grid of size m x n. There are stones placed randomly in some of the squares of this grid.
Design a way to find out minimum rectangular area which covers all the stones in this grid. */
/* WORKING CODE
Input
5 4
0 0 0 0
0 1 0 0
0 0 0 1
1 0 0 0
0 0 0 0
Output: (1,0), (1,3), (3,0), (3,3)
*/
#include<stdio.h>
#define MAX(a,b) (a>b?a:b)
#define MIN(a,b) (a<b?a:b)
int main()
{
int m,n,A[10][10],i,j,i1=10,i2=0,j1=10,j2=0;
printf("enter m & n (m*n) :");
scanf("%d %d",&m,&n);
printf("entet array (place 1 as rock other wise 0 :\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(A[i][j]==1)
{
i1=MIN(i1,i);
i2=MAX(i2,i);
j1=MIN(j1,j);
j2=MAX(j2,j);
}
printf("\n(%d,%d), (%d,%d), (%d,%d), (%d,%d)\n",i1,j1,i1,j2,i2,j1,i2,j2);
return 0;
}
if there will be repeating of word then this algo will give same string multiple times, but one can remove it by comparing either is al-ready printed or not(do-it urself)
-------------------
http://www.geeksforgeeks.org/archives/767
There it is good code
# include <stdio.h>
/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char a[] = "ABC";
permute(a, 0, 2);
getchar();
return 0;
}
Algorithm Paradigm: Backtracking
Time Complexity: O(n*n!)
/* WAP to return all anagrams from the array of strings. */
// WORKING CODE || RECURSIVE
#include<stdio.h>
#include<string.h>
int anagram(char S[10],int l,char C[10])
{
char D[10];
int j,k,m=strlen(C),i;
for(j=0;j<=m;j++)
{
for(k=m;k>j;k--)
D[k]=C[k-1];
D[k]=S[m];
D[m+1]=NULL;
for(k=j-1;k>=0;k--)
D[k]=C[k];
if((m+1)==l)
printf("%s\n",D);
else
anagram(S,l,D);
}
return 0;
}
int main()
{
char S[10],C[10];
C[0]=NULL;
gets(S);
printf("\n");
anagram(S,strlen(S),C);
return 0;
}
/*WAP to return numbered index if input is excel sheet column header name.
e.g excel sheet column headers are A, B, C , D ... Z, AA, AB...AZ, BA,, etc
if Input is D , output should be 4
and for AA output should be 27 */
// WORKING CODE TIME : O(n)
#include<stdio.h>
#include<string.h>
int main()
{
int i=0,l,x,index=0;
char S[5];
printf("Enter Column header : ");
gets(S);
l=strlen(S);
while(i<l)
{
x=S[i++];
index+=x-'A'+1;
}
index+=25*(i-1);
printf("%d",index);
return 0;
}
/* sort an array a[n] containing 0s and 1s with time complexity n and you cant use more than 1 data structure
(you cant use more than 1 array)... the interviewer gave me good 10min to write the code */
// Working Code || TIME: O(n) || Space : O(1)
#include<stdio.h>
int main()
{
int A[20],i,j,n,temp;
printf("Enter n :");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\n--");
i=0;
j=n-1;
while(i<j)
{
while(A[i]==0)
i++;
while(A[j]==1)
j--;
if(i<j)
{
temp=A[i];
A[i]=A[j];
A[j]=temp;
i++; j--;
}
}// end of while
printf("\n");
for(i=0;i<n;i++)
printf("%d ",A[i]);
printf("\n");
return 0;
}
/* find the no. of time a particular word is occurring in a string?
eg. "Hello I am going to I with Hello am"? */
// WORKING CODE
#include<stdio.h>
#include<string.h>
int main()
{
char S[50],W[20][10],C[20];
int i=0,j,l,k=0,A[20]={0},flage=0;
gets(S);
l=strlen(S);
W[0][0]='\0';
while(i<l)
{
j=0;
flage=0;
for(;S[i]!=' ' && i<l;i++)
C[j++]=S[i];
C[j]=NULL;
for(j=1;j<=k;j++)
if(!strcasecmp(C,W[j])) // strcasecmp() return 0 on equal
{
A[j]++;
flage=1;
break;
}
if(!flage)
{
k++;
strcpy(W[k],C);
A[k]++;
}
i++;
}// end of while
printf("\n");
for(i=1;i<=k;i++)
printf("\n%d\t%s",A[i],W[i]);
printf("\n");
return 0;
}
/* reverse a string. "hello my name is" to be displayed as "is name my hello" */
// WORKING CODE || RECURSIVE
#include<stdio.h>
#include<string.h>
int reverse( char C[25])
{
int i;
i=strlen(C);
if(!i)
return 0;
for(i=i-1;i>=0&&C[i]!=' ';i--);
if(i>=0)
C[i]=NULL;
printf("%s ",C+i+1);
if(i>=0)
reverse(C);
return 0;
}
int main()
{
char S[25];
gets(S);
reverse(S);
return 0;
}
Repstacimdalton, Dev Lead at ASAPInfosystemsPvtLtd
At the moment I'm implementing Slinkies in the financial sector. My current pet project is researching break up a ...
Traverse LL and get total no of character "n".
- niraj.nijju October 25, 2012Traverse again starting from root and keep pushing the character into stack until ti reaches to size of n/2, escape 1 character if n is odd. and start traversing from this point while each character comes next pop a character from stack and compare it to current character . if it not same return false.
if any one of the stack or LL end return false
otherwise return true(when both stack and LL ends together )