Prithvi
BAN USERhttp://www.linkedin.com/pub/prithvi-bandhu-adhikari/31/982/342
Email: onlyprithvi@gmail.con
- 2of 2 votes
AnswersCan you predict the output of the following code?
- Prithvi in Nepal for Java Developerfor (int i = 0; i < 101; i++) { if (i % 2 == 0) { System.err.print(i); } else System.out.print(i); }
| Report Duplicate | Flag | PURGE
Software Engineer / Developer Java - 1of 1 vote
AnswersFind the longest substring that is the same in reverse.
As an example, if the input was "I like racecars that go fast"
the answer would be "racecar".
Test your code in the following String:
- Prithvi in United States"FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"
| Report Duplicate | Flag | PURGE
Java Developer Algorithm
a straight forward Java Solution:
public String reverse(String string) {
String stAry[] = string.split(" ");
StringBuilder b = new StringBuilder();
for (int i = stAry.length - 1; i >= 0; i--) {
b.append(stAry[i]).append(" ");
}
return b.toString().substring(0, b.lastIndexOf(" "));
}
Brut force:
the function:
public List findNonOverLapingPair(List<int[]> set) {
List<int[]> temp = new ArrayList<int[]>(set);
List temp2 = new ArrayList();
for (int i = 0; i < set.size(); i++) {
temp.remove(0);
int check[] = set.get(i);
for (int[] is : temp) {
if (((check[0] < is[0] && check[0] < is[1]) && (check[1] < is[0] && check[1] < is[1]))
|| ((check[0] > is[0] && check[0] > is[1]) && (check[1] > is[0] && check[1] > is[1]))) {
Object o[]= {check,is};
temp2.add(o);
}
}
}
return temp2;
}
test code:
public static void main(String[] args) {
List<int[]> set = new ArrayList<int[]>();
int[] a = { 1, 2 };
int[] a2 = { 3, 4 };
int[] a3 = { 3, 6 };
int[] a4 = { 8, 10 };
set.add(a);
set.add(a2);
set.add(a3);
set.add(a4);
List l=new Main().findNonOverLapingPair(set);
for(Object o:l){
Object[] b=(Object[])o;
int[] i=(int[]) b[0];
int[] j=(int[]) b[1];
System.out.println(i[0]+","+i[1]+"::"+j[0]+","+j[1]);
}
}
output:
1,2::3,4
1,2::3,6
1,2::8,10
3,4::8,10
3,6::8,10
The same regex can be used in java.
public class Main {
Pattern p = Pattern.compile("(.)\\1{1}");
Pattern pattern = Pattern.compile("(\\w)\\1+");
public static void main(String[] args) {
Main appMain = new Main();
Set<String> stringSet = new HashSet<String>();
stringSet.add("Dauresselam");
stringSet.add("slab");
stringSet.add("fuss");
stringSet.add("boolean");
stringSet.add("clap");
stringSet.add("clap");
appMain.compare(stringSet);
}
private void compare(Set<String> stringSet) {
for (String arg : stringSet) {
Matcher match = p.matcher(arg);
if (match.find())
System.out.println( " Match found in word:"
+ arg);
}
}
}
You can use any of the 2 regex. they both should work fine
- Prithvi October 06, 2013The problem with multiple inheritance is that: two classes may have different function definition but same function declaration, ie same what to do but different how to do it.
The subclass gets confused in picking the function.
Interfaces specify only what the class is doing, not how it is doing it.
tree
root
/ \
/ \
/ \
/ \
node1 node2
/ \ /\
/ \ / \
/ \ / \
cond...
if you notice here nodes are trees too. the data structure may look something like
t: v [t[1], ..., t[k]]// t is tree, v is the value of the node, t[n] are childs
Map: are just key value pair.
key1 | value1
key2 |value 2
Test case may cover these scenario.
1:test for the device type:
This should return the device type.
public String test(HttpServletRequest request){
return request.getHeader("User-Agent");}
2:test for device connected to wifi:
2.1. check ConnectivityManager type:
ConnectivityManager connManager =
(ConnectivityManager) con.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo current = connManager.getActiveNetworkInfo();
boolean isWifi = current != null && current.getType() == ConnectivityManager.TYPE_WIFI
2.2. try getting SSID
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();// returns null or ssid if connected to any wifinetwork
3 :test for connected to the internet.
- try pinging a web-service
ping google.com// a reply means connected to internet.
32-bit systems can address 4 GB of memory in one go (2^32), 64 bit can address 2^64 bits(16 exbibytes ). If a processor is 64 bit and you are running a 32 bit os, the os will run fine. the extra processing capacity of the processor will be unused. But a 64 bit processor will use all the processing power of the processor.
you cannot run a 64 bit os in a 32 bit processor.
Os all about using the computing capacity of the hardware.
Here is a way that came to my mind.
-Start traversal.
-Keep track of current pointers in the process, Lets say a hash set allHashsets<Pointers>.
-if allHashsets<Pointers> contains next pointer. current element is the last element
in the example: 1-->2-->3-->4-->5-->7-->8-->9-->10-->
check if next pointer is in the hashset? the pointer for 1 will not be in the hash set so this means 1 is not the last element. add the pointer for 1 in the hashset. when the traversal reaches 10, the next pointer will be in the hashset. this means the last element is the current one.
Edited
Equality:
in c, return (a-b); false is returned for equality;// i am still thinking its java solution
Inequality: a -b !Equality 0
for b>a:
if(((a-b) >> 31)=1 and (a inequality b)) a is greater// for other data types (not 32 bit representation) similar shifting.
else b is greater
a/b
c= 0, temp= a;
while(temp>= b){
b-= a;
c++;
}
c is the answer
you have to replace them in-place with what? I think this too straight forward or am i missing something?
i=0;
start iteration;
if char[i] ='a' Flag=1;
if char[i]='b' remove char[i]; reset Flag=0;
if char[i]='c' and Flag=1; remove char[i] and char[i-1]; reset flag;
if char[i] is any other letter; reset flag;
i++;
The answer i gave was the 1st thing that came to my mind. its is not a very good answer after i made the assumption of memory.
To ans gen-y-s: A map to keep track of distance and corresponding coordinates. (i think a Treemap is sorted by key. A tree map requires a value comparator. Am i wrong?)
i agree Sorting the coordinates by the distance in array. i hope you are not taking about sorting of distances alone .
But corsider more than 1 point at same distance.
defining heapsize k might not be good enough:
Consider a case where there are 1 million points, its possible that there are 1million -k cordinates that are at k farthest from origin.
an alter-net approach:
use pointer1 in the start(right). use pointer2 at the end(left). pointer1 moves right to left, pointer2 moves left to right. at any point if the pointer value if don't match its not a palindrome. this check must be done unless the pointer are pointing to same character or consecutive. n/2(even length) or n/2+1(oddlength)
reversing the 2nd part can be confusing incase length of word is odd, eg: bob
an alter-net approach:
use pointer1 in the start(right). use pointer2 at the end(left). pointer1 moves right to left, pointer2 moves left to right. at any point if the pointer value if don't match its not a palindrome. this check must be done unless the pointer are pointing to same character or consecutive. n/2(even length) or n/2+1(oddlength)
Even in single processor there could be time sharing.
eg.
Threads T1 reads the number say "1". times up before it could actually increase the number. T1 waits for the next time slot.
Thread T2 reads the number "1". and increases the number and assign the new value to "2"
T1 gets the time slot. T1 had read the value to be 1. so assign the new value to be 2.
in this operation the value of the variable will be increase by just 1 even if 2 threads are working simultaneously.
Thus the minimum possible value in the will be 100 and max value will be 200
add a step 0 -
- Search only the words whose letter starts with a letters in given word.
Some modification that can be done after can be done after step 2 but i don't think these are elegant:
step 2.1 -store the original words(split-ed) in a array and in a set.
step 2.2 -add the letters in the new word in the set. If the length of the set changes its a miss. those words which pass the tests will require further processing
A note before step 3:-
I am not aware of any hash insensitive to the order. We might take sum of hash of each letters and compare the aggregate hash. a miss means no anagrams. i seriously doubt vice-versa is true.
Finally step3:
if Sorting is not an option, the only way to check new records is to compare each row in new file with rows in old file.
CollectionOfExcelObject oldSetofPerson;// this contains all objects of all old persons info
CollectionOfExcelObject newSetofPerson;//this contains all objects of new persons info
foreach(persons in new newSetofPerson){
if(oldSetofPerson.has(person))
this is old person. continue the loop.
else
this is new person.
}
CollectionOfExcelObject any collection implementation.
- Prithvi May 09, 2013
Should you find only one set of numbers or sets of all numbers? i mean for {1,2,3} the answer would be {-1-2},{-3}. Assuming we need to return all sets of negative numbers,
1. find permutations of the array.
2. for each element in permutation; negate 1st element and take the sum. if sum is 0, return the 1st element, else negate 1st and 2nd elements... and so on...
java POC:
output:
- Prithvi December 07, 2013