Skip to main content
Group

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!

One of my favorite algorithms.

 

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

Integer first = 0;

Integer last = opportunities.size()-1;

while (first <= last) {

Integer middle = first + (last - first) / 2;

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

return middle;

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

last = middle - 1;

else

first = middle + 1;

}

return -1;

}

1 answer
0/9000

#17 - Age Group

public String ageGroup(Integer n) {

return n < 2 ? 'Infant' : n < 15 ? 'Child' : n < 22 ? 'Youth' : 'Adult';

}

2 comments
0/9000

Hey,  

Check out how I implemented problem 55 - Companion plants 2: 

public Boolean companionPlants(List<String> plants) { 

    //code here 

    if(plants.size()==1){ 

        return false; 

    } 

 

    Map<String, List<String>> companionMap = new Map<String, List<String>>(); 

 

    companionMap.put('lettuce', new List<String>{'cucumbers', 'onions'});  

    companionMap.put('onions', new List<String>{'lettuce', 'carrots', 'tomatoes'}); 

    companionMap.put('cucumbers', new List<String>{'lettuce'}); 

    companionMap.put('carrots', new List<String>{'onions'}); 

    companionMap.put('tomatoes', new List<String>{'onions'}); 

 

    for(Integer i = 0; i < plants.size()-1; i++){ 

 

        List<String> currentCompanions = companionMap.get(plants[i]); 

 

        if(!currentCompanions.contains(plants[i+1])){ 

 

            return false; 

 

        } 

 

    } 

 

 

 

    return true; 

 

 

Now I am in top 4 percent of all ApexSandbox users.

0/9000

The "Change Time Format" problem really got me. I tried searching for appropriate methods in the Time and DateTime classes but there I couldn't see a out of the box method or methods that would solve the problem. I see that the hints direct me in different path. Nice "gotcha". :) 

0/9000

Hi, 

I've logged in after the a few days back to ApexSandbox where I completed of about 15 problems but i don't see the code that I composed why is this so? Is uploading to GitHub the only way to store the problems solutions? 

 

Thanks in advance, 

Dimitar Nikolov

3 answers
  1. Apr 14, 9:27 AM

    Hi @Dimitar Nikolov - Salesforce will discard inactive sandboxes for improving the usage. If the sandbox is inactive you will start receiving emails on deactivation and based on the date they'll deactivate it. You need to login the org once in few weeks or have everything in github.

0/9000

ApexSandbox Website is good for solving the problems but I didn't find anything like public profile

similar to the Leetcode.  

Is there any option for public profile ?

 

if not than I think creators should implement that as well. It will boost up the adoption and can make this website as standard for problem solving. 

@Mehdi Maujood

 

what's your thoughts?

0/9000

may be a bit more complex but  

 

public List<sObject> getListOfsObject(List<Account> accounts, List<Contact> contacts)

{

List<sObject> objList = new List<sObject>();

if(accounts == null && contacts == null){

return objList;

}

for(Account acc : accounts?? new List<sObject>()){

objList.add(acc);

}

for(Contact c : contacts?? new List<sObject>()){

objList.add(c);

}

return objList;

}

1 answer
  1. Mar 2, 4:28 PM

    @Marcus Spears

    , I hope we could make it Genral : 

     

    public with sharing class JsonUtil {    public static List<SObject> deserializeSObjects(String inputJson, Schema.SObjectType sObjectType) {        if (String.isBlank(inputJson) || sObjectType == null) {            return new List<SObject>();        }        try {            Type listType = Type.forName('List<' + sObjectType.getDescribe().getName() + '>');            return (List<SObject>) JSON.deserialize(inputJson, listType);        }         catch (Exception e) {            System.debug('Deserialization error: ' + e.getMessage());            return new List<SObject>();        }    }}

     

    Hope that give some idea  

     

    Thank You

0/9000

public List<Account> getAccountsFromJSONString(String inputJSON){

if(inputJson == null){

return null;

}

return (List<Account>) JSON.deserialize(inputJSON, List<Account>.class);

}

1 answer
  1. Mar 2, 4:28 PM

    @Marcus Spears

    , I hope we could make it Genral : 

     

    public with sharing class JsonUtil {

    public static List<SObject> deserializeSObjects(String inputJson, Schema.SObjectType sObjectType) {

    if (String.isBlank(inputJson) || sObjectType == null) {

    return new List<SObject>();

    }

    try {

    Type listType = Type.forName('List<' + sObjectType.getDescribe().getName() + '>');

    return (List<SObject>) JSON.deserialize(inputJson, listType);

    }

    catch (Exception e) {

    System.debug('Deserialization error: ' + e.getMessage());

    return new List<SObject>();

    }

    }

    }

     

    Hope that give some idea  

     

    Thank You

0/9000