Skip to main content
Grupo

Apex Practice Problems - ApexSandbox.io

View and post solutions to programming problems on www.apexsandbox.io, ask questions and get help from a community of people who love to code!

Stumbled across the apexsandbox.io site and just wanted to say thank you for putting it together! There really is not a whole lot out there in terms of apex practice problems and as a new developer, this is definitely helpful for me.

0/9000

#137 - Tip Calculator

Create a function that given a total bill, and an integer percentage value between 0 and 100, computes the tip at the specified percentage. You can assume the percentage specified will be an integer within the valid range.

For an example, computeTip(200.0, 50) evaluates to 100.0, because 50% of 200 is 100.

 

public Decimal computeTip(Decimal total, Integer percent) {

    Decimal Tip;

    Tip = (total * percent) / 100;

    return tip;

}

2 comentarios
  1. 12 sept, 21:44

    The calculation itself looks correct. If total is 200 and percent is 50, then:

    (200 * 50) / 100 = 100

    So I would simplify the method a little:

    public Decimal computeTip(Decimal total, Integer percent) {

    Decimal tip = (total * percent) / 100;

    return tip;

    }

    One small thing to watch is the variable name. You declare Decimal Tip; but return tip;. Depending on the language, capitalization can matter, so keeping it consistently lowercase avoids that issue.

    You could also calculate the percentage separately as (total * percent) / 100, which is the general approach for converting an integer percentage into an amount.

    For example, 15% of 80 would be 12.00.

0/9000

#118 - Find Common Characters

 

public List<String> commonChars(List<String> strs){

//solution here

Integer[] listCount = new Integer[26];

for (Integer i = 0; i < 26; i++)

listCount[i] = 2147483647;

for (String s : strs){

Integer[] wordCount = new Integer[26];

for (Integer i = 0; i < 26; i++)

wordCount[i] = 0;

for (Integer i = 0; i < s.length(); i++){

wordCount[s.charAt(i) - 97] = wordCount[s.charAt(i) - 97] + 1;

}

System.debug(wordCount);

for (Integer i = 0; i < 26; i++)

listCount[i] = Math.min(listCount[i], wordCount[i]);

}

System.debug(listCount);

List<String> res = new List<String>();

for (Integer i = 0; i < 26; i++){

Integer count = listCount[i];

while (count > 0){

count--;

String myChar = String.fromCharArray(new List<integer>{ i + 97 });

res.add(myChar);

}

}

return res;

}

4 comentarios
0/9000

#87 - Cases by Type

public Map<String, List<Case>> casesByType(List<Case> cases) {

Map<String, List<Case>> result = new Map<String, List<Case>>();

for (Case c : cases) {

if (c.Type != null) {

if (result.containsKey(c.Type)) {

result.get(c.Type).add(c);

}

else {

result.put(c.Type, new List<Case> { c });

}

}

}

return result;

}

1 comentario
  1. 9 sept, 23:51

    public static Map<String, List<Case>> casesByType(List<Case> cases) {

     

     Map<String, List<Case>> result = new Map<String, List<Case>>();

     

     for (Case c : cases) {

     if (String.isBlank(c.Type)) {

     continue;

     }

     

    result.put(c.Type, (result.get(c.Type) ?? new List<Case>()));

    result.get(c.Type).add(c);

     }

     

    return result;

    }

0/9000

Posting the solution here as I couldn't find one for this problem

 

public static Integer findLast(List<Integer> nums, Integer target) {

    Integer index;

    if(!nums.contains(target))

    {

        return -1;

    }

    for(Integer i = nums.size()-1; i >= 0; i--)

    {

        if(nums[i] == target)

        {

            index = i;

            break;

        }

    }

    return index;

3 respuestas
  1. 2 sept, 22:42

    //alternate solution 

    for(Integer i = nums.size() - 1; i >= 0; i--) 

    if(nums[i] == target) return i;

    return -1;

0/9000

#80 - Duplicate Integers

public Boolean containsDuplicates(List<Integer> nums) {

Set<Integer> seen = new Set<Integer>();

for (Integer i=0; i<nums.size(); i++) {

if (seen.contains(nums[i])) return true;

seen.add(nums[i]);

}

return false;

}

4 comentarios
  1. 31 ago, 22:34

    Abdul Basith - Acknowledging and thanks for brief code.

    I want to shorten it further: 

    #92 - Sorting a List

     

    public List<String> getNamesInAscOrder(List<String> accountNames)

    {

        accountNames?.sort();

        return accountNames ?? accountNames;

     

    }

0/9000

Desenvolvedor Júnior (exclusiva para pessoas autistas)

 

A Specialisterne, em parceria com uma multinacional de referência em soluções digitais, está com inscrições abertas para pessoas autistas que queiram ingressar no mercado de trabalho.

 

As vagas são direcionadas para:

  •  Pessoas autistas com 18 anos ou mais.
  •  Com conhecimentos e/ou formação na área de desenvolvimento de software.

 

Atividades:

  • Projetar e construir soluções avançadas dentro da plataforma.
  • Garantir que os sistemas sejam escaláveis, tenham alto desempenho e se integrem de forma eficiente com outras plataformas.
  • Será parte ativa do processo de code review.
  • Seguir os fluxos de trabalho já estabelecidos pela equipe para gestão de código e participação nos processos de deployment.

 

Local de Trabalho: Zona Sul - São Paulo

 

Requisitos básicos:

  • Conhecimento em Java.
  • Noções de programação orientada a objetos e lógica de programação.
  • Interesse e motivação para aprender novas tecnologias.

 

Diferenciais (Desejáveis):

  • Apex.
  • Lightning Web Components.
  • Integrações de APIs (REST/SOAP).
  • SOQL e/ou SOSL .
  • Git para controle de versões.
  • Conhecimento de inglês.

 

A contratação será diretamente pela empresa parceira Salesforce.

 

Se você tem mais de 18 anos, atende aos requisitos informados e quer participar desse processo, preencha o formulário de candidatura a seguir:

Formulário de Inscrição

 

Desenvolvedor Júnior (exclusiva para pessoas autistas) A Specialisterne, em parceria com uma multinacional de referência em soluções digitais, está com inscrições abertas para pessoas autistas que que

 

 

0/9000

Reverse Words In String

public static String reverseWords(String str){

// solution here

List<String> revStr = new List<String>();

for(String sp : str.split(' ')){

revStr.add(sp.reverse());

}

return String.join(revStr,' ');

}

0/9000

Binary Search Opportunites

public static Integer search(List<Opportunity> opportunities, Integer target){

// solution here

Integer left = 0;

Integer right = opportunities.size();

while(left < right){

Integer middle = Math.abs((left + right) / 2);

if(opportunities[middle]. Amount == target)

return middle;

if(opportunities[middle].Amount < target){

left = middle + 1;

}else{

right = middle - 1;

}

}

return - 1;

}

0/9000

#120 Merge Two Sorted Lists

public static List<Integer> mergeLists(List<Integer> list1, List<Integer> list2){

// solution

List<Integer> finalLst = new List<Integer>();

Integer maxLen = list1.size() + list2.size();

Integer i = 0;

Integer j = 0;

Integer x = 0;

while(x < maxLen){

if(i > list1.size() - 1){

finalLst.add(list2[j]);

j++;

}else if(j > list2.size() - 1){

finalLst.add(list1[i]);

i++;

}else if(list1[i] < list2[j]){

finalLst.add(list1[i]);

i++;

}else{

finalLst.add(list2[j]);

j++;

}

x++;

}

return finalLst;

}

0/9000