0% found this document useful (0 votes)
3 views

Technical Assessment coding questions

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Technical Assessment coding questions

Uploaded by

subhash.chandra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Technical Assessment

Coding Questions

Examples:
Input: S = “geek”
Output: 28
Explanation:
The value obtained by the sum order of alphabets is 7 + 5 + 5 + 11 = 28.
Input: S = “GeeksforGeeks”
Output: 133

int findTheSum(string alpha)


{
// Stores the sum of order of values
int score = 0;

for (int i = 0; i < alpha.length(); i++)


{
// Find the score
if (alpha[i] >= 'A' && alpha[i] <= 'Z')
score += alpha[i] - 'A' + 1;
else
score += alpha[i] - 'a' + 1;
}
// Return the resultant sum
return score;
}

bool sortele(pair<int, int> a, pair<int, int> b)


{
if (a.second == b.second)
return a.first < b.first;
return a.second > b.second;
}
void Sortelementsbyfreq(int arr[], int n)
{
unordered_map<int, int> map;
for (int i = 0; i < n; i++)
{
map[arr[i]]++;
}
vector<pair<int, int>> vec;
for (auto it = map.begin(); it != map.end(); it++)
{
vec.push_back({it->first, it->second});
}
sort(vec.begin(), vec.end(), sortele);
for (int i = 0; i < vec.size(); i++)
{
while (vec[i].second > 0)
{
cout << vec[i].first << " ";
vec[i].second--;
}
}
cout << endl;
}
int main()
{
int arr[] = {3, 3, 5, 2, 1, 1, 2};
int n = 7;
Sortelementsbyfreq(arr, n);
return 0;
}

Output: 1 1 2 2 3 3 5

Time Complexity - O(N) where N is the number of elements in the


array

Space Complexity - O(N) for storing the elements in Map, vector

You might also like