Microsoft Interview Question
Developer Program EngineersCountry: United States
Interview Type: Phone Interview
U r a good coder..Every1 ll appreciate u.. THANK U ""AnanthaNag KUNDANALA""
CODE:
import java.io.*;
public class Moka
{
public static void main(String args[]){
String sortedNumbers = "123456789 9876543210";
for (int i = 31; i <= 300; i++) {
String temp = "" + i;
if (!sortedNumbers.contains(temp)) {
System.out.println(temp);
}
}}}
OUTPUT:
31
33
35
36
37
38
39
40
41
42
44
46
47
48
49
50
51
52
53
55
57
58
59
60
61
62
63
64
66
68
69
70
71
72
73
74
75
77
79
80
81
82
83
84
85
86
88
90
91
92
93
94
95
96
97
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
isnt anantha's solution more complex .... i mean to check if temp is present in string we could use kmp <at best > ( having O(n)) with some preprocessing that would raise the complexity to above O(n).......
wouldnt it be easy if use the following pseudocode with complexity O(n) :
for(int i=31 to 3000)
if(abs(i-reverse(i))==(9 or 198 or 3087 )
do not print
else
print "i"
package in.neeraj.app.main;
public class PrintNumber {
public static void main(String args[]) {
int j, k, m, x, a, b;
for (int i = 30; i <= 3000; i++) {
if (i < 99) {
j = i / 10;
k = i % 10;
if (j == (k + 1) || j == (k - 1))
i++;
}
else if (i > 99 && i < 1000) {
j = i / 100;
k = i % 100;
m = k / 10;
x = k % 10;
if ((m == (j + 1) && x == (m + 1))
|| (m == (x + 1) && j == (m + 1)))
i++;
}else
{
j = i / 1000;
k = i % 1000;
m = k / 100;
x = k % 100;
a = x / 10;
b = x % 10;
if (m == (j + 1) && a == (m + 1) && b == (a + 1))
i++;
}
System.out.println(" " + i);
}
}
}
public int PrintAll(int s,int e){
int count=0;
for(int i=s;i<=e;i++){
if(i<99){
int j=i/10;
int k=i%10;
if(j==(k+1)||j==(k-1)){
i++;
}
}
else if(i>99&&i<1000){
int j=i/100;//3 or 1
int k=i%100;
int l=k/10;//2 or 2
int m=k%10;//1 or 3
if((j==(l+1)&&l==(m+1))||(m==(l+1)&&l==(j+1))){
i++;
}
}
else if(i>999 && i<=e){
int j=i/1000;//1 or 4
int k=i%1000;
int l=k/100;//2 0r 3
int m=k%100;
int n=m/10;//3 or 2
int o=m%10;//4 or 1
if((j==(l+1)&&l==(n+1)&&n==(o+1))||(j==(l-1)&&l==(n-1)&&n==(o-1))){
i++;
}
}
System.out.println(" "+i);
count++;
}
return count;
}
public class test {
int start = 30;
int end = 3000;
private boolean isAllowed(int i)
{
if (isAsnding(i) || isDecending(i))
{
return false;
}
return true;
}
private boolean isAsnding(int i)
{
int prev = -1 ;
int cur = -1;
boolean result = true;
boolean cond = true;
do{
if (i > 10000)
{
cur = (i/10000);
i = (i%10000);
}
else if (i >1000)
{
cur = (i/1000);
i = (i%1000);
}
else if (i > 100)
{
cur = (i/100);
i = (i%100);
}
else if (i >10)
{
cur = (i/10);
i = (i%10);
}
else if (i <10)
{
cur = i;
cond = false;
}
if (prev != -1 && prev != cur+1 )
result = false;
else
{
prev = cur;
}
}while (cond == true && result == true);
//System.out.println(" asc result: " + result);
return result;
}
private boolean isDecending(int i)
{
int prev = -1 ;
int cur = -1;
boolean result = true;
boolean cond = true;
do{
if (i > 10000)
{
cur = (i/10000);
i = (i%10000);
}
else if (i >1000)
{
cur = (i/1000);
i = (i%1000);
}
else if (i > 100)
{
cur = (i/100);
i = (i%100);
}
else if (i >10)
{
cur = (i/10);
i = (i%10);
}
else if (i <10)
{
cur = i;
cond = false;
}
if (prev != -1 && prev != cur-1 )
result = false;
else
{
prev = cur;
}
}while (cond == true && result == true);
return result;
}
private void printRange()
{
for (int i = start ; i <= end ; i++)
{
if (isAllowed(i))
{
System.out.println(i + " ");
}
}
}
/**
* @param args
*/
public static void main(String[] args)
{
test t = new test();
t.printRange();
}
}
bool valid(int n){
bool increase=false,decreas=false;
int last=n%10,cur=0;
n/=10;
while(n>0){
cur=n%10;
if(cur>last)
decrease=true;
else if (cur<last)
increase=true;
last=cur;
n/=10;
}
return increase^decrease;
}
void nonIncreasingDecreasingNum(int start,int end){
for(int i=min(100,start);i<=end;i++){
if(valid(i))
cou<<i;
}
}
#include<iostream>
using namespace std;
int Fns(int Table[])
{ int flag=0,flag2=0;
int n;
for(int i=30;i<=300;i++)
{ flag=0;flag2=0;
n=i;
while(n)
{
Table[n%10]+=1;
if(Table[n%10]>1)
{ flag=2;
break;
}
n/=10;
}
if(flag==0)
for(int j=0;j<10;j++)
{
if(Table[j])
{
if(flag2==1)
{flag=2;
break;
}
flag=1;
}
else
{ if(flag==1)
flag2=1;
}
}
if(flag==2)
cout<<i<<" ";
for(int j=0;j<10;j++)
Table[j]=0;
}
}
int main()
{
int Table[10]={0};
Fns(Table);
return 0;
}
package test;
public class TestClass {
public static void main(String[] args) {
for (int input = 300; input < 3000; input++) {
int mul = 10,currDigit = 0;
boolean flag = true,increasing = false, decreasing = false;
int lastDigit = (input) % 10;
while((input/mul) > 0 && flag)
{
currDigit = (input/mul) % 10;
if(currDigit == lastDigit + 1 && !increasing)
{
decreasing = true;
}
else
decreasing = false;
if(currDigit == lastDigit -1 && !decreasing)
{
increasing = true;
}
else
increasing = false;
if(!increasing && ! decreasing)
flag = false;
mul*=10;
lastDigit = currDigit;
}
if(!flag)
System.out.println(input);
}
}
}
#include "stdafx.h"
#include <cmath>
#include <iostream>
#include <cstdlib>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 31; i < 3000; i++)
{
int st[4] = {};
st[3] = i % 10;
st[2] = (i % 100) / 10;
st[1] = (i % 1000) / 100;
st[0] = i / 1000;
int p = 0;
for (int j = 0; j < 3; j++)
{
if (st[j] != 0)
{
p = p + abs(st[j + 1] - st[j]);
}
}
int db = log(i) / log(10);
if (p != db)
{
cout << i << endl;
}
system("pause");
}
return 0;
}
Yes, the code is very elegant, but is it more efficient than a more straightforward one? As I know, the KMP String Matcher takes O(m+n) time, where m is the number's length, and n is the length of "123456789 9876543210". While for the straightforward one, we could just scan the number's length and determine whether it is a valid number, which will takes only O(m).
Here is my code in C++ for the two version:
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
void compute_prefix_func( const std::string& pattern, std::vector<int>& prefix_func )
{
int m = pattern.size();
prefix_func.resize(m+1,0);
if( m <= 0 ) return;
int k = 0;
for( int i = 1; i < m; ++i )
{
while( k > 0 && pattern[i] != pattern[k] ) k = prefix_func[k];
if( pattern[i] == pattern[k] ) ++k;
prefix_func[i+1] = k;
}
}
bool kmp_matcher( const std::string& pattern, const std::string& target )
{
int m = pattern.size(), n = target.size();
if( m > n || m == 0 || n == 0 ) return false;
std::vector<int> prefix_func;
compute_prefix_func( pattern, prefix_func );
int k = 0;
for( int i = 0; i < n; ++i )
{
while( k > 0 && pattern[k] != target[i] ) k = prefix_func[k];
if( pattern[k] == target[i] ) ++k;
if( k == m ) return true;
}
return false;
}
//KMP one
int valid_number()
{
std::stringstream os;
int count = 0;
for( int i = 30; i <= 3000; ++i )
{
os.str(""), os.clear();
os << i;
if( !kmp_matcher( os.str(), "123456789 9876543210" ) ) std::cout << i << std::endl, ++count;
}
return count;
}
//straightforward one
int valid_number2()
{
std::stringstream os;
std::string str;
int count = 0;
bool increasing, decreasing;
for( int i = 30; i <= 3000; ++i )
{
increasing = decreasing = true;
os.str(""), os.clear();
os << i;
str = os.str();
for( int j = 0; j < str.size() - 1 && ( increasing || decreasing ); ++j )
{
if( str[j] - str[j+1] != -1 ) decreasing = false;
if( str[j] - str[j+1] != 1 ) increasing = false;
}
if( (!increasing) && (!decreasing) ) std::cout << i << std::endl, ++count;
}
return count;
}
int main( int argc, char* argv[] )
{
std::cout << valid_number2() << std::endl;
return 0;
}
#include<stdio.h>
#include<conio.h>
#define N1 30
#define N2 300
int main()
{
int i, j=0, A[N2],a=0,b=0,flag=0,temp;
for(i=N1;i<N2;i++)
{
temp=i;
while(temp>0)
{
a=temp%10;
if(a-b == 1 || a-b == -1)
flag=1;
else
flag=0 ;
b=a;
temp=temp/10;
}
if(flag==0)
A[j++]=i;
}
for(i=0;i<j;i++)
{
printf("%d ", A[i]);
}
getch();
return 0;
}
For an invalid number the abs diff of the first and the last digit is equal to its length minus 1. If the length is odd the mid digit is equal to the (first + last)/2
Examples:
56 1
5678 3
7654 3
3456789 6, and 6 = (3 + 9)/2
This would help to recursively check for the (in)validity of the number.
- AnanthaNag KUNDANALA September 25, 2013