Search Your Question

Write program for ascending sorting Int Array in any language

Ans : 
(Interviewer told syntax doesn't matter, write pseudo-code type program)

1. Not using direct function.

 void main()
    {

        int i, j, a, n, number[30];
        printf("Enter the value of N \n");
        scanf("%d", &n);

        printf("Enter the numbers \n");
        for (i = 0; i < n; ++i)
            scanf("%d", &number[i]);

        for (i = 0; i < n; ++i)
        {

            for (j = i + 1; j < n; ++j)
            {

                if (number[i] > number[j])
                {

                    a =  number[i];
                    number[i] = number[j];
                    number[j] = a;

                }

            }

        }

        printf("The numbers arranged in ascending order are given below \n");
        for (i = 0; i < n; ++i)
            printf("%d\n", number[i]);

    }

2. If we write in swift using higher order function, then

var numbers = [45,6,113,56,8,56,43,78]

print(numbers.sort()) //Sorting in Ascending order

print(numbers.sort(>)) //Sorting in Descending order

Interviewer mostly asked this question to check your programming logic. So he/she don't require syntax.

Q. Sorting strings array
A.
Method 1 : 
var sortedArray = swiftArray.sorted { $0.localizedCaseInsensitiveCompare($1) == ComparisonResult.orderedAscending }

Method 2 : 
let sortedNames = names.sort { $0.name < $1.name }
let sortedNames = names.sorted(by: <)


Difference between Swift Array and Objective C Array

Ans : Swift arrays (Array and for short, []) are passed by value which means
that every object contained will be copied.

NSArray are implemented as classes (and bridged from ObjC) so they're passed as references.

In Swift, we can declare three type of array.
One is Array or [],
Second is NSArray,
Third is NSMutableArray.

NSArray and NSMutableArray are coming from Objective C by bridge. So interviewer may ask what are difference between types arrays in swift?

Extract value from Array : 

Get 3rd Value from Array

Objective C : [arr objectAtIndex:2] // 2 due to index start from 0 in array

Swift : arr[2] 

Equilibrium Problem - Left and right side and top and bottom side sum of any point are same in matrix


Problem Description :

#import <Foundation/Foundation.h>


int leftArraySum(NSMutableArray *A,int n)
{
    NSInteger sum  = 0;
    for(int i=0;i<n;i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int reightArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=n+1;i<[A count];i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int topArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=0;i<n;i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int bottomArraySum(NSMutableArray *A,int n)
{
     NSInteger sum  = 0;
    for(int i=n+1;i<[A count];i++)
    {
        sum += [[A objectAtIndex:i] integerValue];   
    }
    return sum;
}

int solution(NSMutableArray *A) {
    // write your code in Objective-C 2.0
 
    NSMutableArray *rowArr = [A objectAtIndex:0];
    int colCount = [rowArr count];
    int rowCount = [A count];
 
    NSMutableArray *arrRowSum = [[NSMutableArray alloc] initWithCapacity: rowCount];
    NSMutableArray *arrColSum = [[NSMutableArray alloc] initWithCapacity: colCount];
 
    for(int i=0;i<rowCount;i++)
    {
        NSArray *colArr = [A objectAtIndex:i];
        NSNumber * sum = [colArr valueForKeyPath:@"@sum.self"];
        [arrRowSum insertObject:sum atIndex:i];
    }
 
    for(int i=0;i<colCount;i++)
    {
     
        NSInteger sum = 0;
        for(int j=0;j<rowCount;j++)
        {
           sum +=  [[[A objectAtIndex:j] objectAtIndex:i] integerValue];
        }
        NSNumber *sumVal = [NSNumber numberWithInt:sum];
        [arrColSum insertObject:sumVal atIndex:i];
    }
 
    int result = 0;
    for (int i=1;i<rowCount;i++)
    {
        int left = leftArraySum(arrRowSum,i);
        int right = reightArraySum(A,i);
        if(left  == right)
        {
            for(int j=0;j<colCount;j++)
            {
                int top = topArraySum(A,j);
                int bottom = bottomArraySum(A,j);
                   if(top == bottom )
                {
                    result = i+j;
                    break;
                }
            }
        }
    }
 
    return result;
}


What types of feature google map provide to developers?

Ans : 

Google map features :
  • Plotting places Near Me. Like near by hospitals, restaurants, temples
  • Direction ( route ), Distance, Travel time, Live traffic
  • Street view for 3D location
  • Geofencing
  • Live tracking location
  • Get current location
  • Put markers on various places
Alternatives of Google map api :
I am using MapMyIndia api due to high cost of Google map apis. I have also implemented here map. Both are nice.

There are two versions of here-map, free and premium. Premium here map service is better than free here map service. I have implemented live tracking of car, replay route, using both apis.

What are collection types in objective C and Swift?

Ans : 

In  iOS, there are 3 collection types

i. Array 
ii. Dictionary 
iii. Set.

There are two types of collection types. Mutable and Immutable. Mutable can be changed but immutable can not be changed once initialised.

Q. How to check nil keys in dictionary?
A. There is not any nil keys in dictionary.