Join Regular Classroom : Visit ClassroomTech

Salesforce – Overall Interview Questions + Coding Solutions – codewindow.in

Hot Topics

Salesforce Solution

Technical Round

#include <bits/stdc++.h>
using namespace std;

void reverseWords(string s){
	vector<string> tmp;
	string str = "";
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == ' ') {
			tmp.push_back(str);
			str = "";
		}
		else
			str += s[i];
	}
	tmp.push_back(str);
	int i;
	for (i = tmp.size() - 1; i > 0; i--)
		cout << tmp[i] << " ";
	cout << tmp[0] << endl;
}

int main()
{
	string s = "i like this program very much";
	reverseWords(s);
	return 0;
}
/*
OUTPUT -
much very program this like i
*/
def repeated_element(arr):
    start, end = 0, len(arr) - 1
    while start <= end:
        mid = (start + end) // 2
        if arr[mid] == arr[mid-1] or arr[mid] == arr[mid+1]:
            return arr[mid]
        elif arr[mid] == mid:
            start = mid + 1
        else:
            end = mid - 1
    return None
This function takes a sorted array arr as input and returns the element that repeats. The basic idea is to use binary search to find a middle element in the array and compare it with its neighboring elements. If the middle element is equal to one of its neighboring elements, then it is the repeating element. If the middle element is equal to its index, then the repeating element must be in the second half of the array, so we update start to mid + 1. If the middle element is not equal to its index or its neighboring elements, then the repeating element must be in the first half of the array, so we update end to mid - 1. The function continues to divide the array in half and check the middle element until the repeating element is found.
def count_distinct_numbers(arr):
    return len(set(arr))
This function takes an array arr of integers as input and returns the number of distinct integers in the array by converting the array into a set and then returning the length of the set. Since sets only allow unique elements, converting the array into a set eliminates the duplicates, and the length of the set represents the number of distinct elements in the original array.
#include <bits/stdc++.h>
using namespace std;

class Node{
    public:
    int data;
    Node* next;
    Node(int d){
        this->data=d;
        this->next=NULL;
    }
};

void insertAtHead(Node* &head, int d) {

    // new node create
    Node* temp = new Node(d);
    temp -> next = head;
    head = temp;
}

void display(Node* &head) {
    if(head == NULL) {
        cout << "List is empty "<< endl;
        return ;
    }
    Node* temp = head;
    while(temp != NULL ) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
}

Node* middleNode(Node* head){
    if(head==NULL || head->next==NULL){
        return head;
    }
    if(head->next->next==NULL){
        return head->next;
    }
    Node *fast=head,*slow=head;
    while(fast!=NULL){
        fast=fast->next;
        if(fast!=NULL){
            fast=fast->next;
        }
        slow=slow->next;
    }
    return slow;
}

int main() {
    
    Node* node = new Node(10);
    Node* head = node;
    Node* curr = head;
    Node* prev = NULL;
    
    insertAtHead(head,20);
    insertAtHead(head,30);
    insertAtHead(head,40);
    insertAtHead(head,50);
    insertAtHead(head,60);
    
    display(head);
    cout<<head->data<<endl;
    Node* mid=middleNode(head);
    cout<<"Middle node is: "<<mid->data<<endl;

    return 0;
}
/*
OUTPUT 
60 50 40 30 20 10 
60
Middle node is: 30
*/
public static boolean isPalindrome(int num) {
    if (num < 0) {
        return false;
    }
    int reversed = 0, original = num;
    while (num > 0) {
        int digit = num % 10;
        reversed = reversed * 10 + digit;
        num /= 10;
    }
    return reversed == original;
}
In this code, we first check if the input number is negative, in which case we return false because negative numbers can’t be palindromes. Then, we reverse the number using a while loop by extracting each digit, adding it to a variable reversed, and updating num to be num / 10. Finally, we return true if the reversed number is equal to the original number, and false otherwise. This way, we can check if a given number is a palindrome by calling isPalindrome(num).
public TreeNode findIndex(TreeNode root, int index) {
    if (root == null) {
        return null;
    }
    int leftCount = countNodes(root.left);
    if (leftCount == index) {
        return root;
    } else if (leftCount > index) {
        return findIndex(root.left, index);
    } else {
        return findIndex(root.right, index - leftCount - 1);
    }
}

private int countNodes(TreeNode node) {
    if (node == null) {
        return 0;
    }
    return 1 + countNodes(node.left) + countNodes(node.right);
}
In this code, the function findIndex uses a helper function countNodes to count the number of nodes in the left subtree. If the number of nodes in the left subtree is equal to the desired index, then the current node is the desired node. If the number of nodes in the left subtree is greater than the desired index, then the desired node is in the left subtree. If the number of nodes in the left subtree is less than the desired index, then the desired node is in the right subtree, and the desired index is updated to index - leftCount - 1.
#include <bits/stdc++.h>
using namespace std;

class Stack{
	queue<int> q;
	public:
		void Push(int x){
			//getting the size of queue
			int size=q.size();
			//pushing our element into the queue
			q.push(x);
			//using a for loop to iterate through the elements in queue
			for(int i=0;i<=size-1;i++){
				/*for size one lesser than the size of queue we are
 getting the elements from the front of queue(FIFO) and pushing it back into the same queue.*/
				q.push(q.front());
				//removing the front element from queue
				q.pop();
			}
		}
		
		int Pop(){
			/*getting element from front of queue as we know queue 
			follows FIFO principle*/
			int element=q.front();
			//removing the front element from queue
			q.pop();
			//giving back the element that we removed from queue
			return element;
		}

		int Top(){
			/*the top element in stack will be the first or front element
			of queue*/
			return q.front();
		}
		int Display(){
      			queue<int> p=q;
      			while(!p.empty()){
          			int n=p.front();
          			cout<<n<<endl;
          			p.pop();
      			}
     	 		return 0;
  		}
};//end of class 
	
int main(){
	//s is an object of class Stack on which we will perform our operations
	Stack s;
	//pushing element into the stack
	s.Push(1);
  	s.Push(2);
  	s.Push(3);
  	s.Push(4);

  	cout<<"Displaying the elements in stack"<<endl;
    s.Display();
    cout << "Top element of the stack is: " << s.Top() << endl;
  	cout << "Deleted element is: " << s.Pop() << endl;
	cout<<"Displaying the elements in stack"<<endl;
    s.Display();
  	cout << "Top element of the stack after removing element is: " <<
 	s.Top() << endl;
}
/*
OUTPUT -
Displaying the elements in stack
4
3
2
1
Top element of the stack is: 4
Deleted element is: 4
Displaying the elements in stack
3
2
1
Top element of the stack after removing element is: 3
*/
public char firstNonRepeating(char[] chars) {
    Map<Character, Integer> map = new HashMap<>();
    for (char c : chars) {
        map.put(c, map.getOrDefault(c, 0) + 1);
    }
    for (char c : chars) {
        if (map.get(c) == 1) {
            return c;
        }
    }
    return ' ';
}
In this code, we use a hash map map to store the frequency of each character. We iterate over the input array of characters and increment the frequency of each character in the hash map. Then, we iterate over the input array again and return the first character whose frequency is 1 in the hash map. If there is no non-repeating character, we return a space character.
public List<Integer> intersection(int[] nums1, int[] nums2) {
    Set<Integer> set = new HashSet<>();
    List<Integer> result = new ArrayList<>();
    for (int i : nums1) {
        set.add(i);
    }
    for (int i : nums2) {
        if (set.contains(i)) {
            result.add(i);
            set.remove(i);
        }
    }
    return result;
}
In this code, we use a hash set set to store the elements of nums1. Then, we iterate over nums2 and check if each element exists in the hash set. If it does, we add it to the result list and remove it from the hash set to avoid duplicates. The final result is returned as a list of integers.
#include <bits/stdc++.h>
using namespace std;

class Node{
    public:
    int data;
    Node* next;
    Node(int d){
        this->data=d;
        this->next=NULL;
    }
};

void insertAtHead(Node* &head, int d) {

    // new node create
    Node* temp = new Node(d);
    temp -> next = head;
    head = temp;
}

void print(Node* &head) {
    if(head == NULL) {
        cout << "List is empty "<< endl;
        return ;
    }
    Node* temp = head;
    while(temp != NULL ) {
        cout << temp -> data << " ";
        temp = temp -> next;
    }
    cout << endl;
}

void reverse(Node* &head,Node* curr,Node* prev){
    if(curr==NULL){
        head=prev;
        return ;
    }
    Node* forward=curr->next;
    reverse(head,forward,curr);
    curr->next=prev;
    print(forward);
}

int main() {
    
    Node* node = new Node(10);
    Node* head = node;
    Node* curr = head;
    Node* prev = NULL;
    
    insertAtHead(head,20);
    insertAtHead(head,30);
    insertAtHead(head,40);
    
    print(head);
    cout<<head->data<<endl;
    reverse(head,curr,prev);

    return 0;
}
/*
OUTPUT -
40 30 20 10 
40
List is empty 
*/

      

Go through our study material. Your Job is awaiting.

Recent Posts
Categories