Juspay Coding Question
Juspay Coding Question
Shivina wants to design a game for kids for their enjoyment as well as their knowledge . She
takes the string from the children and sorts the string by the property such that the character
which has the highest frequency will come first and the character which has the least frequency
will come last. Now you have to write a code for that and help shivina for getting the sorted
string according to the frequency which will have to be higher to lower.
Write an algorithmic code for this.
Constraint
Input : s = "cccaaa"
Output : "cccaaa"
Explanation :
a and c have same frequency 3 , but a come before c in alphabet.
Input1 : s = "ccaaa"
Output1 : "aaacc"
Solution in C++:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
unordered_map<char,int> m;
for(int i=0;i<s.size();i++){
m[s[i]]++;
}
vector<pair<int,char>> vect;
for(auto v : m){
vect.push_back({ v.second,v.first});
}
sort(vect.begin(),vect.end(),greater<pair<int,char>>());
string str;
for(auto v: vect){
string temp(v.first,v.second);
str=str+ temp;
}
cout<<str;
return 0;
}
Solution in Java:
import java.util.*;
Q2 : Problem Statement :
Ram, who have scares to stars, found himself constantly bothered by his friend's liking for them.
One day, his friend presented him with a game designed to help overcome his fear. In this
game, Ram is presented with a string containing a mix of characters and stars. His objective is
to select a star and eliminate both the star itself and the nearest non-star character to its left.
This process continues until there are no stars left in the string. Once all stars are removed,
Ram must return the remaining string. Can you help Ram by writing an algorithmic code ?
Constraints :
● 1<= s.length <=105
● S consists of lowercase english letters and star *
Input 0 :
"leet**cod*e"
Output 0 :
"Lecoe"
Input 1 :
"erase*****"
Output 1 :
""
Input 2 :
"leet**cod*"
Output 2 :
"Leco"
Input 4 :
"erase***"
Output 4 :
“er”
Solution in C++:
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
string ans;
for(int i=0;i<ans.size();i++){
cout<<ans[i];
}
return 0;
}
Solution in Java:
import java.util.*;
return stack.toString();
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
System.out.println(solution(str));
scan.close();
}
}
Q3 : Problem Statement :
The school principal gathered students from different classes. They had to say their roll
numbers and line up in order from smallest to largest. They were to count consecutive
subsequences in this line. The main goal was to identify the longest consecutive sequence of 'n'
initial roll numbers. This activity was to make school fun and help students practice math and
organization
Your task is to write an algorithmic code to find the longest consecutive sequence.
Definition of consecutive sequence: Numbers that follow each other continuously in the order
from smallest to largest are called consecutive numbers.
Constraints :
Inputs :
6 = number of elements
[100,4,200,1,3,2] = elements
Output :
Explanation :
In ascending order : 1,2,3,4,100,200
Maximum consecutive sequence : 1,2,3,4 = 4
Solution in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<int>nums;
for(int i=0;i<n;i++){
int t;
cin>>t;
nums.push_back(t);
}
vector<int> ans;
int counter = 1;
int maxi = INT_MIN;
if(nums.size() == 0)
return 0;
else if(nums.size() == 1){
cout<<1;
return 0;
}
sort(nums.begin(),nums.end());
ans.push_back(nums[0]);
for(int i = 1;i < nums.size();i++){
if(ans.back() + 1 == nums[i]){
ans.push_back(nums[i]);
counter++;
}else if(ans.back()+1 != nums[i] && ans.back() != nums[i]){
ans.clear();
ans.push_back(nums[i]);
maxi = max(maxi,counter);
counter = 1;
}
maxi = max(maxi, counter);
}
cout<<maxi;
return 0;
}
Solution in Java:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> nums = new ArrayList<>();
for (int i = 0; i < n; i++) {
int t = sc.nextInt();
nums.add(t);
}
if (nums.size() == 0) {
System.out.println(0);
return;
} else if (nums.size() == 1) {
System.out.println(1);
return;
}
Collections.sort(nums);
ans.add(nums.get(0));
for (int i = 1; i < nums.size(); i++) {
if (ans.get(ans.size() - 1) + 1 == nums.get(i)) {
ans.add(nums.get(i));
counter++;
} else if (ans.get(ans.size() - 1) + 1 != nums.get(i) && ans.get(ans.size() - 1) !=
nums.get(i)) {
ans.clear();
ans.add(nums.get(i));
maxi = Math.max(maxi, counter);
counter = 1;
}
maxi = Math.max(maxi, counter);
}
System.out.println(maxi);
}
}