Interview Question


Country: United States




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

Maintain a hashtable of char and int ... where int is the number of times a char occurs

- Anonymous March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

please can u explain what this code is doing...means plz explain the logic

- pramod March 26, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char str[]="AJDHURNANDANDJNAUYTRAYAUIA ";
int count[256];
int max_no=0;
char max;
for(i=0;i<256;i++)
count[i]=0;
for(int i=0;str[i]!='/n';i++)
count['Z'-str[i]+1]++;
for(i=0;i<256;i++)
if(max_no<count[i]){
max_no=count[i];
}
}
for(i=0;i<256;i++)
if(max_no==count[i]){
max=(char)('Z'+1-i);
cout<<max<" "<<max_no;

}
}

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

char cal_max_occur(char str[])
{
int hashArr[256]={0};
char MaxChar = str[0];
for (int i = 0; str[i] != '\0'; i++)
{
hashArr[int(str[i])]++;
if(hashArr[int(MaxChar)] < hashArr[int(str[i])])
MaxChar = str[i];
}
return MaxChar;
}

- HSS March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// considering only A - Z letters
#include <stdio.h>
#define MAXSIZE 100
int main()
{
 char str[MAXSIZE];
 int count[26]={0};
 int i,max,indx=0;

 printf("enter string : ");
 gets(str);
 
 for(i=0;str[i]!='\0';i++)
 {
  count[(int)str[i]-65]++;
 }
 
 max=count[0];
 for(i=0;i<26;i++)
 {
  if(max<count[i])
  {
   max=count[i];
   indx=i; 
  }   
 }
 printf("max repeated char is : %c\n",(char)(65+indx));
 return(0);
}

- venky March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this does not include small letters in english.. only capital letters are considered

- alien March 31, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
void main()
{
char a[26],var[100],max=0,pos=0,i=0;
printf("\nEnter the string:");
scanf("%s",var);
for(i=0;i<26;i++)
a[i]=0;
while(var[i]!='\0')
{
a[var[i]-65]++;
i++;
}
for(i=0;i<26;i++)
{
if(a[i]>max)
{
max=a[i];
pos=i;
}
}
printf("max occurance char is %c and %d times.",pos+65,max);
getch();
}

PLZ CORRECT IT . NOT GIVE THE CORRECT NO. OF CHAR OCCUR

- nivi.keshri1111 March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxOcurrance(char[] charAr){
int max, i, indx = 0;
int count[] = new int [26];

for (i = 0; i < charAr.length; i++) {
count[(int)charAr[i]-65]++;
}

max = count[0];
for(i=0;i<26;i++)
{
if(max<count[i])
{
max=count[i];
indx=i;
}
}

return indx;
}

System.out.println((char)(65+st.maxOcurrance(charArray)));

- Santosh March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
public class CharacerReputution {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int counter =1, arycounter=0;
String s1 ="aabcdaefbbbbbb";
Map<Character,Integer> m1 = new HashMap<Character,Integer>();
char chArry[] = new char[6];
for(int i=0;i<s1.length();i++)
{
if(m1.containsKey(s1.charAt(i)))
{
counter = m1.get(s1.charAt(i));
counter += 1;
m1.remove(s1.charAt(i));
m1.put(s1.charAt(i), counter);
}
else
{
counter =1;
m1.put(s1.charAt(i), counter);
chArry[arycounter] = s1.charAt(i);
arycounter += 1;
}
}
int max=0,bet=0;
char c;
max = m1.get(chArry[0]);
c = chArry[0];
for(int i=1; i< chArry.length-1; i++)
{
if(m1.containsKey(chArry[i]))
{
bet = m1.get(chArry[i]);
if( max < bet)
{ max = bet;
c = chArry[i];
}
}
}

System.out.println(" max charac =" + c + " rep= "+ max);
}
}

- prashanthreddy.mtech March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this code is simple and easy to understand

- lal April 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<stdlib.h>
int main()
{
static int max,j,i,a[26];
char d[40];
printf("enter the string in small letter:\n");
scanf("%s",d);
while(d[i])
++a[d[i++]-97];
max=a[0];
for(i=1;i<26;i++)
{
if(max<a[i]){
max=a[i];j=i;}
}
printf(" the maximum occurance of character is :%c",j+97);
return 0;
}

- ram rs March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

-have an array 2D of size unique occurrence of charX2 for ex Array[26X2]
-fetch list and pick first char
-check whether picked char is present in array or not
-in case not present add in array say at position Array [12,0]
---Also add occurrence 1 at Array [12,1]
-in case picked char is present in array lets say at Array[15,0]
--then just increase the occurrence by one at Array[15,1]
-At last we will get all unique char and their occurrence in Array

This sol is helpful in case unique occurrence of element in Array are limited not so large

- PKT March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is there any 2d Array, which accepts character and Integer, other than collections. Correct me if i am wrong.

- prashanthreddy.mtech March 27, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Prashanth:
-In case choosing programming language as java we can have string 2D array and can handle numeric to string and string to numeric conversion.
OR
-we can have 2 array one for char and another for int. while operating arrays we will use a common index for both of the arrays. this sol will work for both c and java.

- PKT March 28, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int maxOccurance(String strValue) {
		char [] arrCh = strValue.toCharArray();
		Map<String, Integer> mpCharCount = new HashMap<String, Integer>(); 
		int iMaxValue = 0;
		for (char cValue : arrCh) {
			String strEachValue = String.valueOf(cValue);
			int iCount = mpCharCount.containsKey(strEachValue) ? (mpCharCount.get(strEachValue) + 1) : 1;
			mpCharCount.put(strEachValue, iCount);
			if(iMaxValue < iCount) iMaxValue = iCount;
		}
		return iMaxValue;
}

- rizwan.amd March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findHighestOccur(String str) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		int max = 1;
		char maxChar = ' ';
		
		for(int i=0; i<str.length(); i++) {
			String s = str.substring(i, i+1);
			if (map.containsKey(s)){
				int nextCount = map.get(s).intValue()+1;
				if (nextCount > max) {
					max = nextCount;
					maxChar = s.charAt(0);
				}
				map.put(s, new Integer(nextCount));
			} else {
				map.put(s, new Integer(1));
			}
		}
		System.out.println(maxChar + ":" + max);
	}

- lin March 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int maximum(int a[])
{
int i=1;
int max;
max=a[0];
while(a[i]!=0)
{
if(a[i]>max)
{
max=a[i];
i++;
}
else
{
i++;
}
}
return max;
}
main()
{
char *s="AAAABBBB";
int t,i;
int a[10]={0};
while(*s)
{
i=*s-'A';
a[i]++;
s++;
}
t=maximum(a);
i=0;
while(i!=11)
{
if(t==a[i])
{
printf("%c",i+'A');
break;
}
else
{
i++;
}
}
getch();
}

- Anonymous April 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Working code

#include<iostream>
#include<map>
int main() {
    std::string s("AJDHURNANDAINDJNAUYTRAYAUIA");
    std::map<char, int> chmap;

    for(std::string::iterator it = s.begin(); it != s.end(); ++it) {
        if(chmap.find(*it) != chmap.end()) {
            chmap[*it] += 1;
        } else {
            chmap[*it] = 1;
        }
    }

    char maxchar;
    int maxtimes = 0;
    for(std::map<char, int>::iterator i = chmap.begin(); i != chmap.end(); ++i) {
       if (i->second > maxtimes) {
           maxchar = i->first;
           maxtimes = i->second;
       }
    }

    std::cout << maxchar << " occurs " << maxtimes << " which is the highest\n";
    return 0;
}

- Anonymous April 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()

{
char a[10];
gets(a);
int i=0;
int k=strlen(a);
int b[100];

for(int i=0;i<100;i++)
{
	b[i]=1;
}
while(a[i]!='\0')
{
for(int j=i+1;j<k;j++)
{
	if(a[i]==a[j])
	{
		b[i]=b[i]+1;
	}
}
i++;
}
for(int i=0;i<100;i++)
{
	
	int p;
	for(p=i+1;p<100;p++)
	{
		if(b[i]<b[p])
		{
			break;
		}
	}
	if(p==100)
	{
		cout<<"string is  "<<a[i]<<endl;
		cout<<b[i]<<endl;
		break;
	}
	i++;	
}
getch();
}

- Saurabh Gupta April 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Console.WriteLine("Enter first name:");
string sname1 = Console.ReadLine();
int count = 0, count1 = 0,count2=0;
ArrayList alist=new ArrayList();


for (int i = 0; i < sname1.Length; i++)
{
for (int j = i + 1; j < sname1.Length; j++)
{
if (sname1[i] == sname1[j])
{
count++;



}
}
if (count > 0)
{
count2 = count; count = 0;
if (count1 <= count2) { count1 = count2; alist.Add(sname1[i]); }
}
}

Console.WriteLine("More Occured alphabet is:");

foreach (char x in alist)
{
Console.WriteLine(x);

}
Console.ReadLine();


}

- BHIKSHAM REDDY April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Console.WriteLine("Enter first name:");
string sname1 = Console.ReadLine();
int count = 0, count1 = 0,count2=0;
ArrayList alist=new ArrayList();
for (int i = 0; i < sname1.Length; i++)
{
for (int j = i + 1; j < sname1.Length; j++)
{
if (sname1[i] == sname1[j])
{ count++; }
}
if (count > 0)
{
count2 = count; count = 0;
if (count1 <= count2) { count1 = count2; alist.Add(sname1[i]); }
}
}
Console.WriteLine("More Occured alphabet is:");
foreach (char x in alist)
{ Console.WriteLine(x); }
Console.ReadLine();
}

- Anonymous April 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

namespace HighestOccurrencesOfCharacters
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter any string:");
            string s = Console.ReadLine();

            Dictionary<char, int> result = new Dictionary<char, int>();
            result = HighestOccurrencesOfCharacters(s);

            var max = result.Aggregate((l, r) => l.Value > r.Value ? l : r).Key;
            int numberOfOccurrence = result[max];

            Console.WriteLine("Highest Occuring Character is: {0}\n\nNumber of Occurrence: {1}", max, numberOfOccurrence);
            Console.ReadLine();
        }

        protected static Dictionary<char, int> HighestOccurrencesOfCharacters(string s)
        {
            Dictionary<char, int> result = new Dictionary<char, int>();
            int value;

            foreach (char ch in s)
            {
                if (result.TryGetValue(ch, out value))
                {
                    result[ch] = value + 1;
                }
                else
                {
                    result.Add(ch, 1);
                }
            }
            return result;
        }
    }
}

- Nish May 13, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CounterLetters {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String temp = args[0];
		
		//letter and number of occurrences
		int [] countof = new int [temp.length()];
		int occurrences = 0;
		
		for(int i = 0; i< temp.length(); i++)
		{
			occurrences = 0;
			for(int x = 0; x < temp.length(); x++)
			{
				if(temp.charAt(i) == temp.charAt(x) )
				{
					occurrences++;
					countof[i]= occurrences;
				}
					
			}
		}
		
		
		int biggest = 0;
		
		for(int y = 0; y < countof.length-1; y++)
		{
			if(countof[biggest] > countof[y])
			{
				
			}
			else
				biggest = y;
		}
		
		System.out.println(temp.charAt(biggest));

	}

}

- Jeff August 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package swing;
import java.util.Scanner;

class MaxOccurences
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a string");
String sent=s.nextLine();
sent = sent.toUpperCase();
System.out.println(sent);
int len = sent.length();
int arr[]=new int[26];
int max=0;
char ch=0;
for(int i=0;i<len;i++)
{
arr[sent.charAt(i)-65] = arr[sent.charAt(i)-65]+1;

if(arr[sent.charAt(i)-65]>max)
{
max = arr[sent.charAt(i)-65];
ch=sent.charAt(i);
}
}
System.out.println("Max occured char is: "+ch+" , times: "+max);
}

}

- Anonymous October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package swing;
import java.util.Scanner;
class MaxOccurences
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter a string");
String sent=s.nextLine();
sent = sent.toUpperCase();
System.out.println(sent);
int len = sent.length();
int arr[]=new int[26];
int max=0;
char ch=0;
for(int i=0;i<len;i++)
{
arr[sent.charAt(i)-65] = arr[sent.charAt(i)-65]+1;

if(arr[sent.charAt(i)-65]>max)
{
max = arr[sent.charAt(i)-65];
ch=sent.charAt(i);
}
}
System.out.println("Max occured char is: "+ch+" , times: "+max);
}

}

- Deepak sharma October 15, 2014 | 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