Neo
BAN USER
- 3of 3 votes
AnswersWrite a program to sort an array of strings so that all anagrams are next to each other
- Neo in United States for Web Service
ex
input {god, dog, abc, cab, man}
output {abc, cab, dog, god, man}| Report Duplicate | Flag | PURGE
Amazon Intern C++ Java - 0of 0 votes
AnswersWhat should be the output of the following code.
- Neo in United States for Web Service
class Test {
public int i=0;
@Override
public int hashCode() {
return i;
}
}
Class a{
psvm(){
HashMap <Test, String> hm = new HashMap();
Test t1 = new Test();
hm.put(t1,”success”);
sysout(hm.get(t1)); //print success
t1.i = 10;
sysout(hm.get(t1)); //NULL
}
}| Report Duplicate | Flag | PURGE
Amazon Intern Hash Table
You cannot take help of any of the data structure. This is what I figured out.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package strcheck;
/**
*
* @author Sushant
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String mainString = "abcdefghcd"; //'This is an Example';
String find = "cde";
if (mainString ==null || find ==null|| (mainString.length()<=find.length())) {
return;
}
int count = 0, j = 0;
boolean flag = false;
for(int i = 0 ; i < mainString.length(); i++)
{
if(mainString.charAt(i) == find.charAt(j))
{
int temp = i+1;
for(j = 1; j < find.length(); j++)
{
if(mainString.charAt(temp)== find.charAt(j))
{
temp++;
flag = true;
}
else
{
flag = false;
break;
}
}
if(flag)
{
count++;
}
}
}
System.out.println(" "+count);
}
}
The code works fine
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package trailingzeros;
import java.util.Scanner;
/**
*
* @author Sushant
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int n;
int i = 0, x = 5;
int trailZero = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any factorial number to find trailing zeros.");
n = sc.nextInt();
//for(int i = 1 ; i <=n ; i++)
while(x < n)
{
trailZero += n / x;
x = x*5;
}
System.out.println("Trailing zeros = "+trailZero);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package findsumfromarray;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.util.Hashtable;
/**
*
* @author Sushant
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
int[] arr = { 4,-4,9,2,1, -1, 2, 3, -6, 4, 6, 11,5,0,0,0 };
//the input number
int testNumber = 5;
Program p = new Program();
p.Numbers(arr, testNumber);
//console.ReadKey();
}
}
class Program
{
public void Numbers(int[] arr, int testNumber)
{
int i = 0;
Hashtable ht = new Hashtable();
for (i = 0; i < arr.length; ++i)
{
int secondNumber = (testNumber - arr[i]);
if (!ht.containsKey(arr[i]))
{
if (!ht.containsKey(secondNumber))
{
ht.put(arr[i], secondNumber);
}
else
ht.put(arr[i], ' ');
}
//else
// ht.put(arr[i], ' ');
//System.out.println("Hastable list "+ht.get(arr[i]));
}
System.out.println("Hastable key " +ht.keySet());
System.out.println("Get value at "+ht.get(9));
}
}
The code works fine ..... Try running it ..
- Neo February 28, 2013The while loops runs only once... and in that both pointers are incremented, so its one pass.
nodeType *slow = head;
nodeType *fast = head;
while(fast != null)
{
fast = fast -> link;
(i%2 == 0)
{
slow = slow -> link;
}
}
This way slow points to middle at the end. When fast reaches null.
Repkaylafgioia, Cloud Support Associate at Absolute Softech Ltd
Hi, I am Kayla from Elkins USA. I am working as Project management and I have professional 3-year experience in ...
Its a Fibonacchi seq.. Can be solved by recursion in FIB.
- Neo March 05, 20131st step - 1 way to climb
2nd step - 2 ways
3rd step - 3 ways (Take one then take 2 steps or take 2 or take 1 step or take one step each. Hence total 3 ways)
4th step - will have 5 ways
and so on ...
If you look into it ... it will be a fib series.
Please post any suggestions
Thanks.