Amazon Interview Question for Quality Assurance Engineers


Team: AmazonMusic
Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
0
of 0 vote

def toDict(strA):
setA = set(list(strA))
dict={}
for char in setA:
dict.update({char:strA.count(char)})
print(dict)

- Shuohao January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def toDict(strA):
    setA = set(list(strA))
    dict={}
    for char in setA:
        dict.update({char:strA.count(char)})
    print( dict )

- Shuohao January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def toDict(strA):
    setA = set(list(strA))
    dict={}
    for char in setA:
        dict.update({char:strA.count(char)})
    print(dict)

- raulliu07 January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static string Calculatefrequency(string chars) {
            Dictionary<char, int> freq = new Dictionary<char, int>();
            foreach(var c in chars) {
                // update the char value(counter) if its in dictionary
                if (freq.ContainsKey(c)) {
                    int counter;
                    freq.TryGetValue(c, out counter);
                    counter = counter + 1;
                    freq[c] = counter;
                } else {
                    freq.Add(c, 1);
                }
            }
            string result = "";
           foreach(var f in freq) {
               result += "(" + f.Key + "," + f.Value + ")";
           }

           return result;
        }

- megaNaj January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <string.h>
using namespace std;

int main() {
	char str[10000];
	cin>>str;
	int count[26]={0};
	bool charvisit[26]={false};
	int l=strlen(str);
	for(int i=0;i<l;i++)
	    count[str[i]-97]++;
	for(int i=0;i<l;i++)
	{
	    if(charvisit[str[i]-97]==false)
	    {
	        cout<<char(str[i])<<" "<<count[str[i]-97]<<endl;
	        charvisit[str[i]-97]=true;
	    }
	}
	return 0;
}

- pbox January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

public void charCounter(String word) {
LinkedHashMap<Character, Integer> charcount = new LinkedHashMap<>();
for (int i = 0; i < word.length(); i++) {
if (charcount.get(word.charAt(i)) == null) {
charcount.put(word.charAt(i), 1);
} else {
charcount.put(word.charAt(i), charcount.get(word.charAt(i)) + 1);
}}

for (Map.Entry<Character, Integer> count : charcount.entrySet()) {
System.out.print(count.getKey() + "(" + count.getValue() + ")");
}}

- valdiz777 January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think the code orders the elements as they appear in the original array.

a should come before b and b should appear before c.....

- ranganath.mr February 17, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

using hashtable

- zr.roman January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string s; 
    cin>>s; 
    int n=s.size(); 
    int freq[26]; 
    for(int i=0;i<n;i++)  { 
        freq[s[i]-'a']++; 
    } 
    for(int i=0;i<26;i++)  { 
        if(freq[i]) 
        { 
            cout<<char(i+'a')<<" "<<freq[i]<<"\n"; 
        }
    }

- tejaspandey2010 January 27, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;

public class Test {
	public static void main(String[] args) {
		String test = "avarakedavra";
		printCount(test);
	}

	private static void printCount(String test) {
		Map<Character, Integer> lhm = new LinkedHashMap<>();
		char[] characters = test.toCharArray();
		for (Character a : characters) {
				if(lhm.containsKey(a)){
					lhm.put(a, lhm.get(a)+1);
				}else{
					lhm.put(a, 1);
				}
		}
		for(Character a : lhm.keySet()){
			System.out.println("("+a+", "+lhm.get(a)+")");
		}

	}

}

- devanharikumar89 January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void CountingOfCharacters(string str)
{
vector<sDataArray> data;
for(int i = 0; i < strlen(str.c_str()); i++)
{
char ch = str.at(i);
bool found = false;
std::vector<sDataArray>::iterator it = data.begin();
for (; it != data.end(); ++it)
{
if(it->ch == ch)
{
found = true;
it->count++;
}
}

if(!found)
{
sDataArray temp;
temp.ch = ch;
temp.count = 1;
data.insert(it,temp);
}

}

cout << "(" ;
for (std::vector<sDataArray>::iterator it = data.begin();it != data.end(); ++it)
{
cout << "(" << it->ch << "," << it->count << ")" ;
}
cout << ")";
}

- Nagappa January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void CountingOfCharacters(string str){
vector<sDataArray> data;
for(int i = 0; i < strlen(str.c_str()); i++){
char ch = str.at(i);
bool found = false;
std::vector<sDataArray>::iterator it = data.begin();
for (; it != data.end(); ++it){
if(it->ch == ch){
found = true;
it->count++;}}

if(!found){
sDataArray temp;
temp.ch = ch;
temp.count = 1;
data.insert(it,temp);}}

cout << "(" ;
for (std::vector<sDataArray>::iterator it = data.begin();it != data.end(); ++it){
cout << "(" << it->ch << "," << it->count << ")" ;}
cout << ")";
}

- Nagappa January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void CountingOfCharacters(string str){
vector<sDataArray> data;
for(int i = 0; i < strlen(str.c_str()); i++){
char ch = str.at(i);
bool found = false;
std::vector<sDataArray>::iterator it = data.begin();
for (; it != data.end(); ++it){
if(it->ch == ch){
found = true;
it->count++;}}
if(!found){
sDataArray temp;
temp.ch = ch;
temp.count = 1;
data.insert(it,temp);}}
cout << "(" ;
for (std::vector<sDataArray>::iterator it = data.begin();it != data.end(); ++it){
cout << "(" << it->ch << "," << it->count << ")" ;}
cout << ")";}

- Nagappa January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi

- Nagappa January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
- Nagappa January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Scala implementation:

def main(args: Array[String]) {
    val s = "abbcdc"

    println(s.groupBy(x => x).mapValues(_.length))
  }

- akmo75 January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Or this:

def main(args: Array[String]) {
    val s = "abbcdc"

    val map = mutable.Map[Char, Int]()
    s.toCharArray.foreach(c => map.update(c, map.getOrElse(c, 0) + 1))
    println(map)
  }

- akmo75 January 28, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int  calculateFrequency(char* p)
{
  int frequency[256];
  int i= 0;
  int firstItem=0;
  bzero(frequency,256*sizeof(int));
  for(i=0;i<strlen(p);i++)
  {
     frequency[p[i]]++;
  }
  printf("(");;
  for(i=0;i<strlen(p);i++)
  {
    if(frequency[p[i]]!= 0)
    {
      if(firstItem==0)
      {
      printf("(%c,%d)",p[i],frequency[p[i]]);
      firstItem=1;
      }
      else
      {
      printf(",(%c,%d)",p[i],frequency[p[i]]);
      }
    }
    frequency[p[i]]=0;
  }
  printf(")");;
}
int main()
{
   calculateFrequency("aaabbbDD");
}

- Anonymous January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int calculateFrequency(char* p)
{
int frequency[256];
int i= 0;
int firstItem=0;
bzero(frequency,256*sizeof(int));
for(i=0;i<strlen(p);i++)
{
frequency[p[i]]++;
}
printf("(");;
for(i=0;i<strlen(p);i++)
{
if(frequency[p[i]]!= 0)
{
if(firstItem==0)
{
printf("(%c,%d)",p[i],frequency[p[i]]);
firstItem=1;
}
else
{
printf(",(%c,%d)",p[i],frequency[p[i]]);
}
}
frequency[p[i]]=0;
}
printf(")");;
}
int main()
{
calculateFrequency("aaabbbDD");
}

- Anonymous January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int calculateFrequency(char* p)
{
int frequency[256];
int i= 0;
int firstItem=0;
bzero(frequency,256*sizeof(int));
for(i=0;i<strlen(p);i++)
{
frequency[p[i]]++;
}
printf("(");;
for(i=0;i<strlen(p);i++)
{
if(frequency[p[i]]!= 0)
{
if(firstItem==0)
{
printf("(%c,%d)",p[i],frequency[p[i]]);
firstItem=1;
}
else
{
printf(",(%c,%d)",p[i],frequency[p[i]]);
}
}
frequency[p[i]]=0;
}
printf(")");;
}
int main()
{
calculateFrequency("aaabbbDD");
}

- mka January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int calculateFrequency(char* p)
{
int frequency[256];
int i= 0;
int firstItem=0;
bzero(frequency,256*sizeof(int));
for(i=0;i<strlen(p);i++)
{
frequency[p[i]]++;
}
printf("(");;
for(i=0;i<strlen(p);i++)
{
if(frequency[p[i]]!= 0)
{
if(firstItem==0)
{
printf("(%c,%d)",p[i],frequency[p[i]]);
firstItem=1;
}
else
{
printf(",(%c,%d)",p[i],frequency[p[i]]);
}
}
frequency[p[i]]=0;
}
printf(")");;
}
int main()
{
calculateFrequency("aaabbbDD");

}

- mka January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a = "Salesforce is the best company to work for"

hash = Hash.new(0)

a.downcase.split("").each do |letter|
hash["#{letter}"] += 1
end

hash.each do |key, value|
print "(#{key},#{value}),"
end

- Nick January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<Map<Character, Integer>> getFrequencyOfChars(String input) {
List<Map<Character, Integer>> list = new ArrayList<Map<Character, Integer>>();
Map<Character, Integer> map = new TreeMap<Character, Integer>();
int count;

for (int i = 0; i < input.length(); i++) {
count = 0;
for (int j = 0; j < input.length(); j++) {
if (input.charAt(i) == input.charAt(j)) {
count++;
}
}
map.put(input.charAt(i), count);
}
list.add(map);
return list;
}

- Ankit January 29, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <cstdio>
#include <string.h>

//-----------------------------------------------------------------------------

#define NUM_OF_CHARS 0xFF

int main(int argc, char* argv[]) {
    if (argc <= 1) {
        perror("no input provided!");
        return 1;
    }

    printf ("num of chars: %d\n", NUM_OF_CHARS);
    char *input = argv[1];
    unsigned short output[NUM_OF_CHARS] = {};
    int count = 0;

    do {
        char ch = input[count++];
        if ('\0' == ch)
            break;
        else
            ++output[(unsigned int)ch];

    } while (true);

    for (unsigned char i = 0; i < NUM_OF_CHARS; ++i)
    {
        if (((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') || i == ' ')
            && output[(unsigned int)i] != 0)
            printf ("(%c, %d), ", i, output[(unsigned int)i]);
    }

    return 0;
}

- Anonymous January 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string s = "aaabcdfcc";
            var chars = s.ToCharArray();

            var dic = new Dictionary<char, string>();
            for (int i = 0; i < chars.Length; i++)
            {
                if (dic.ContainsKey(chars[i]) == false)
                {
                    dic.Add(chars[i], chars.Count(p => p == chars[i]).ToString());
                }
            }

- Anonymous February 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string s = "aaabcdfcc";
var chars = s.ToCharArray();

var dic = new Dictionary<char, string>();
for (int i = 0; i < chars.Length; i++)
{
if (dic.ContainsKey(chars[i]) == false)
{
dic.Add(chars[i], chars.Count(p => p == chars[i]).ToString());
}
}

- Vipin February 01, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void combo(String str){

char[] array=str.toCharArray();

HashMap<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<array.length-1;i++){
if(!map.containsKey(array[i])){
map.put(array[i], 1);
}
else
map.put(array[i],map.get(array[i])+1);
}

for(Character a : map.keySet()){
System.out.println(a +" && "+ map.get(a));
}
}

- sumit.kaul.87 February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

C++ Solution

#include<iostream>
#include<map>
#include<vector>

using namespace std;
#if 0
void countLetters(string &in)
{
        map<char,unsigned int> countMap;
        for (unsigned int i = 0; i < in.size(); i++) {
                countMap[in[i]] ++;
        }
        for (map<char, unsigned int>::iterator it = countMap.begin(); it != countMap.end(); it++) {
                cout << "(" << (*it).first << "," << (*it).second << ") - ";
        }
}
#endif

void countLetters(string &in)
{
        vector<unsigned int> hashTable(255, 0);
        for (unsigned int i = 0; i < in.size(); i++) {
                hashTable[in[i]] ++ ;                                                                                                                                                                                                                                          
        }                                                                                                                                                                                                                                                                      
        for (int i = 0; i < 255; i++) {                                                                                                                                                                                                                                        
                if (hashTable[i] > 0) {                                                                                                                                                                                                                                        
                        cout << "(" << static_cast<unsigned char>(i) << "," << hashTable[i] << ") - ";                                                                                                                                                                         
                }                                                                                                                                                                                                                                                              
        }                                                                                                                                                                                                                                                                      
}
int main ()
{
        string str = "abbcccdddeeeea";
        countLetters(str);                                                                                                                                                                                                                                                     
        return 0;

}

- utkubulkan February 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;

namespace FindCharacterFrequency
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "abbcdc";
            var freq = GetCharacterFrequency(input);
            
            ToString(freq);
            
            Console.ReadLine();
        }

        public static Dictionary<char, int> GetCharacterFrequency(string input)
        {
            if (input == null) return null;
            
            if (input.Length == 0) return null;

            if(input.Length == 1)
            {
                var dict = new Dictionary<char, int>();
                dict.Add(input.ToCharArray()[0], 1);
                return dict;
            }

            var charArray = input.ToCharArray();
            var freqList = new Dictionary<char, int>();
            foreach (var ch in charArray)
            {
                if (freqList.ContainsKey(ch))
                {
                    freqList[ch]++;
                }
                else
                {
                    freqList.Add(ch, 1);
                }
            }
            return freqList;
        }

        public static void ToString(Dictionary<char, int> freq)
        {
            foreach (var item in freq)
            {
                Console.WriteLine("({0},{1})", item.Key, item.Value);
            }
        }
    }
}

- Ajay February 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;

namespace FindCharacterFrequency
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "abbcdc";
            var freq = GetCharacterFrequency(input);
            
            ToString(freq);
            
            Console.ReadLine();
        }

        public static Dictionary<char, int> GetCharacterFrequency(string input)
        {
            if (input == null) return null;
            
            if (input.Length == 0) return null;

            if(input.Length == 1)
            {
                var dict = new Dictionary<char, int>();
                dict.Add(input.ToCharArray()[0], 1);
                return dict;
            }

            var charArray = input.ToCharArray();
            var freqList = new Dictionary<char, int>();
            foreach (var ch in charArray)
            {
                if (freqList.ContainsKey(ch))
                {
                    freqList[ch]++;
                }
                else
                {
                    freqList.Add(ch, 1);
                }
            }
            return freqList;
        }

        public static void ToString(Dictionary<char, int> freq)
        {
            foreach (var item in freq)
            {
                Console.WriteLine("({0},{1})", item.Key, item.Value);
            }
        }
    }
}

- ajay.majgaonkar February 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

stringChars = list('abbcdc')
list({key: stringChars.count(key) for key in stringChars}.items())

Output: [('b', 2), ('c', 2), ('a', 1), ('d', 1)]
Aka: Why I like using python :p

- Anonymous February 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

stringChars = list('abbcdc')
print(list({key: stringChars.count(key) for key in stringChars}.items()))

Output: [('b', 2), ('c', 2), ('a', 1), ('d', 1)]
Aka Why I Like Python.

- Anonymous February 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

order n^2 and out of order result!
Aka why you don't know algorithms

- sjjpo2002 February 18, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap<Character, Integer> map = new HashMap<Character, Integer>();
String s = "java";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
Integer val = map.get(new Character(c));
if (val != null) {
map.put(c, new Integer(val + 1));
} else {
map.put(c, 1);
}
}
System.out.println(map);

- Anonymous February 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap<Character, Integer> map = new HashMap<Character, Integer>();
String s = "java";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
Integer val = map.get(new Character(c));
if (val != null) {
map.put(c, new Integer(val + 1));
} else {
map.put(c, 1);
}
}
System.out.println(map);

- AAAA February 17, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
given a string, calculate the frequency of characters, output the array with the letter and frequency. 
(such as: for “abbcdc”, the output should be (a,1),(b,2),(c,2),(d,1))
 */

package algorithm;

public class CharFrequency {
	
	private String str = "abbcdcb";
	private char[] charArr = str.toCharArray();
	
	public void countFreq(char[] arr){
		int count = 0;
		for(int i = 0; i<=arr.length-1; i++){
			count = 0;
			//char tempChar = arr[i];
			for(int j = 0; j <= arr.length-1; j++){
				if(arr[i] == arr[j]){
					count++;
				}
				
			}
			
			System.out.print(" (" + arr[i] + "," + count + ")");
		}
	}

	public static void main(String[] args) {
		CharFrequency obj = new CharFrequency();
		obj.countFreq(obj.charArr);

	}

}

- astha February 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void fun2(char *str){
	int *arr = (int*)calloc(sizeof(int)* 128, 1);
	while (*str) 
		arr[*str++]++;
	for (int i = 0; i < 128; i++)
		if (arr[i] != 0)
			printf("(%c %d)\n", i, arr[i]);
}

- praveen pandit March 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class FrequencyCalculator {
    public static void calculator(String str) {
        int[] ch = new int[26];
        for (int i = 0; i < str.length(); i++) {
            ch[str.charAt(i) - 'a']++;
        }

        for (int i = 0; i < ch.length; i++) {
            if (ch[i] > 0) {
                System.out.print("(" + (char) (i + 'a') + ", " + ch[i] + "),");
            }
        }
        System.out.println("");
    }
}

public class CalculateFrequency {

    public static void main(String[] args) {
        FrequencyCalculator.calculator("abbcdc");
    }
}

- Anonymous March 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class FrequencyCalculator {
    public static void calculator(String str) {
        int[] ch = new int[26];
        for (int i = 0; i < str.length(); i++) {
            ch[str.charAt(i) - 'a']++;
        }

        for (int i = 0; i < ch.length; i++) {
            if (ch[i] > 0) {
                System.out.print("(" + (char) (i + 'a') + ", " + ch[i] + "),");
            }
        }
        System.out.println("");
    }
}

public class CalculateFrequency {

    public static void main(String[] args) {
        FrequencyCalculator.calculator("abbcdc");
    }
}

- Scorpion King March 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main (String[] args){
        int[] counter = new int[52];

        String input = "AAbbbccccddeegg";

        char[] inputArray = input.toCharArray();

        for (char c : inputArray){
            int index = ((int) c) - ((int) 'A');
            counter[index]++;
        }

        for (int i =0; i< counter.length; i++){
            if (counter[i] > 0){
                System.out.print("(" + ((char) ('A' + i)) + "," + counter[i] + ") ");
            }
            System.out.flush();
        }

    }

- seventhmoon August 16, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package prudhvi;

import java.util.HashMap;
import java.util.HashSet;

public class Apple {

public static void main(String[] args) {
HashMap<Character,Integer> hs=new HashMap<>();
String s="abbbcdddc";
int count=0;
for(int i=0;i<s.length();i++)
{
count=0;
for(int j=0;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j))
{
count++;
}
}
hs.put(s.charAt(i),count);

}
System.out.println(hs);
}
}

- prudhvi March 18, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "abbcdcab";
for ( int i = 0; i<str.length(); i++){
char chr = str.charAt(i);
int flag = 0;
for(int k =0; k<i; k++){
if (str.charAt(k)==chr)
flag = 1;
}
if (flag ==0){
int c = 1;
for(int j=i+1; j<str.length(); j++){
if(str.charAt(j)==chr){
c = c+1;
}
}
System.out.print("("+chr+","+c+")"+" ");

}
}

}

- Anonymous June 02, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class MAIN {


public static void main(String[] args) {


String str1= new String() ;
Scanner scn= new Scanner(System.in);
System.out.println("Enter String To calculate");
str1=scn.nextLine();
printArrayFrq(str1);


}

public static void printArrayFrq(String test)
{
char ch[]=test.toCharArray();
Map<Character,Integer> arr=new LinkedHashMap<>();
for(Character a: ch)
{
if(arr.containsKey(a))
{

arr.put(a, arr.get(a)+1);

}
else
{
arr.put(a,1);

}
}

for(Character a:arr.keySet())
{

System.out.println("("+a+","+arr.get(a)+")");


}
}


}

- Somnath December 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class MAIN {

public static void main(String[] args) {

String str1 = new String();
Scanner scn = new Scanner(System.in);
System.out.println("Enter String To calculate");
str1 = scn.nextLine();
printArrayFrq(str1);

}

public static void printArrayFrq(String test) {
char ch[] = test.toCharArray();
Map<Character, Integer> arr = new LinkedHashMap<>();
for (Character a : ch) {
if (arr.containsKey(a)) {

arr.put(a, arr.get(a) + 1);

} else {
arr.put(a, 1);

}
}

for (Character a : arr.keySet()) {

System.out.println("(" + a + "," + arr.get(a) + ")");

}
}

}

- Somnath December 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;

public class MAIN {
public static void main(String[] args) {
String str1 = new String();
Scanner scn = new Scanner(System.in);
System.out.println("Enter String To calculate");
str1 = scn.nextLine();
printArrayFrq(str1);
}
public static void printArrayFrq(String test) {
char ch[] = test.toCharArray();
Map<Character, Integer> arr = new LinkedHashMap<>();
for (Character a : ch) {
if (arr.containsKey(a)) {
arr.put(a, arr.get(a) + 1);
} else {
arr.put(a, 1);
}}
for (Character a : arr.keySet()) {
System.out.println("(" + a + "," + arr.get(a) + ")");
}}}

- Somnath December 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class MAIN {
public static void main(String[] args) {
String str1 = new String();
Scanner scn = new Scanner(System.in);
System.out.println("Enter String To calculate");
str1 = scn.nextLine();
printArrayFrq(str1);
}
public static void printArrayFrq(String test) {
char ch[] = test.toCharArray();
Map<Character, Integer> arr = new LinkedHashMap<>();
for (Character a : ch) {
if (arr.containsKey(a)) {
arr.put(a, arr.get(a) + 1);
} else {
arr.put(a, 1);}}
for (Character a : arr.keySet()) {
System.out.println("(" + a + "," + arr.get(a) + ")");}}}

- Somnath December 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
	String a = "abbcdc";
	HashMap<Character,Integer> result = new HashMap<Character, Integer>();
	char[] ch = a.toCharArray();
	for (int i = 0; i < ch.length; i++) {
		int originalSize = a.length();
		int trimmedSize = a.replace(String.valueOf(ch[i]), "").length();
		result.put(ch[i],originalSize-trimmedSize);
	}
	System.out.println(result);
	}

- Abhimanyu January 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		String s = "abbcdc";
		//Output should be like (a,1), (b,2), (c,2), (d,1)
		LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<>();
		for(int i=0; i<s.length(); i++) {
			if(lhm.containsKey(s.charAt(i))) {
				lhm.put(s.charAt(i), lhm.get(s.charAt(i)) + 1);
			} else {
				lhm.put(s.charAt(i), 1);
			}
		}
		Set<Character> keySet = lhm.keySet();
		String finalVal = "";
		for(Character ch:keySet) {
			finalVal += "(" + ch + "," + lhm.get(ch) + "),";
		}
		System.out.println(finalVal.substring(0, finalVal.length()-1));
	}

- GK May 03, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		String s = "abbcdc";
		//Output should be like (a,1), (b,2), (c,2), (d,1)
		LinkedHashMap<Character, Integer> lhm = new LinkedHashMap<>();
		for(int i=0; i<s.length(); i++) {
			if(lhm.containsKey(s.charAt(i))) {
				lhm.put(s.charAt(i), lhm.get(s.charAt(i)) + 1);
			} else {
				lhm.put(s.charAt(i), 1);
			}
		}
		Set<Character> keySet = lhm.keySet();
		String finalVal = "";
		for(Character ch:keySet) {
			finalVal += "(" + ch + "," + lhm.get(ch) + "),";
		}
		System.out.println(finalVal.substring(0, finalVal.length()-1));
	}

- GK May 03, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More