Amazon Interview Questions

Amazon Interview Questions

Amazon is world's largest online retailer and cloud computing services, provider. Read Best interview questions and answers on Amazon. Following are the list of some Amazon's frequently asked questions for candidates who were preparing for amazon job interview.

Download Amazon Interview Questions PDF

Amazon Interview Questions

There is a number of ways to reverse a string in programming. Here we going to see how to reverse a string in C language.

Note: Below programs are written in C language. It takes a string as input from users and reverses it.

For example: if a user inputs hello then it outputs olleh

Different ways to reverse a string

Anagram strings are those strings which have same letters but in different orders. Method to check: First, sort the strings and then compare the sorted strings. If strings are equal then they are anagram strings otherwise not.

Program:

#include <stdio.h>
#include <string.h>
int main () { 
  char str1[] = "listen";   
char str2[] = "silent";
char temp;  
 int i, j;  
 int n1  = strlen(str1);  
 int n2 = strlen(str2);  
 //strings are not anagrams if they are of different length
  if( n1 != n2) {    
printf("%s and %s are not anagrams! \n", str1, str2);   
return 0; } 
for(i=0; i<n1-1; i++) {  // first sort both strings 
for (j = i+1; j < n1; j++) {     
if (str1[i] > str1[j]) {            
temp= str1[i];           
str1[i] = str1[j];       
str1[j] = temp;         }       
if (str2[i] > str2[j]) {  
temp  = str2[i];        
str2[i] = str2[j];           
str2[j] = temp;         }
}   }  
for(i = 0; i<n1; i++) {  // Compare character by character both strings
if(str1[i] != str2[i]) {         
printf("Strings are not anagrams \n", str1, str2);         
return 0;      
}}   
printf("Strings are anagrams! \n");   return 0;}

Basic method to find a pair whose sum matches with given sum:

C program:

#include <iostream>
#include <algorithm>
 // Function to find a pair  using Sorting in an array with given sum
void findPair(int arr[], int n, int sum){
     std::sort(arr, arr + n);   // sort the array in ascending order
    int i = 0;
    int j = n - 1;
     while (i < j) {   // loop till i is less than j
        if (arr[i] + arr[j] == sum){  // sum found
       std::cout << "Pair found"<<”\n”<< arr[i]<<arr[j];
       return;}
       // decrement j if total is more than the sum
         // increment i  if total is less than the sum
      (arr[i] + arr[j] < sum)? i++: j--;}
    // No pair exists in an array with the desired sum
    std::cout << "Pair not found";}
// Main function from which find pair function is called
int main()
{ int arr[] = { 8, 7, 2, 5, 3, 1};
 int sum = 10;
 int n = sizeof(arr)/sizeof(arr[0]);
 findPair(arr, n, sum);
return 0;}