• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Wrong Directions given find minimum moves so that he can reach to the destination

July 20, 2014 by Dhaval Dave

A person wants to go from origin to a particular location, he can move in only 4 directions(i.e East, West, North, South) but his friend gave him a long route, help a person to find minimum Moves so that he can reach to the destination.
Input – NESNWES
Output – E

E.g – SSSNEEEW
output – EESS

Explanation : its very easy code :
1st way to think is with parenthesis matching there you have to match { , [ , ( and closing of it.
While here you have just four directions N,S,E,W.
instead of using Stack and increasing memory usage, we can take two variables say “north” and “east” which will take care of N,S and E,W.
for every occurrence of N or E, increment north or east by one.
and decrease for occurrences of  S and W.
Finally in both north and east variable, there will be some value, just convert them in Direction and print it.

Thanks to Dhaval for suggesting this approach and Code

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int NSValue(char exp){
if (exp == 'N' or exp == 'n') return 1;
else if(exp == 'S' or exp == 's') return -1;
}//NSValue
int EWValue(char exp){
if (exp == 'E' or exp == 'e') return 1;
else if(exp == 'W' or exp == 'w') return -1;
}//EWValue

int getDirection(char exp[]){
int east=0;
int north=0;
int i,j,temp,l;
l=strlen(exp);
for(i=0;i<l;i++){
 if((exp[i]=='N' or exp[i]=='n') || (exp[i]=='S' or exp[i]=='s')){north+=NSValue(exp[i]);}
 if((exp[i]=='E' or exp[i]=='e') || (exp[i]=='W' or exp[i]=='w')){east+=EWValue(exp[i]);}
}//for
if(east>0){for(i=0;i<east;i++){printf("%c",'E');}}//if
else if(east<0){east=-east;
 for(i=0;i<east;i++){printf("%c",'S');}
}//if


if(north>0){for(i=0;i<north;i++){printf("%c",'N');}}//if
else if(north<0){north=-north;
 for(i=0;i<north;i++){printf("%c",'S');}
}//elseif

}//getDirection
int main() {
 //char exp[100] = "NESNWES";
 char exp[100] = "SSSNEEEW";
 getDirection(exp);
 return 0;
}

You can check running code at http://ideone.com/e.js/U4yd4W

Similar Articles

Filed Under: Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Array, codechef

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Find next greater number with same set of digits

building with N steps, we can take 1,2,3 steps calculate number of ways to reach at top of building

Find the kth number with prime factors 3, 5 and 7

In Given LinkedList Divide LL in N Sub parts and delete first K nodes of each part

Find shortest distances between every pair of vertices ( Dynamic Programming Floyd Warshall Algorithm)

Check a String is SUBSEQUENCE of another String Find Minimum length for that ( DNA Matching )

Find Pythagorean Triplets in an array in O(N)

C Program for TAIL command of UNIX

Serialise Deserialise N-ary Tree

Memory Efficient LinkedList

Adobe Interview Questions 8 month Exp

Trapping Rain Water

Best Java Book | Top Java Programming Book for Beginners

Maximum of all subarrays of size k

Sort Stack in place

Find min element in Sorted Rotated Array (Without Duplicates)

Printing Longest Common Subsequence

Password Predictor

Leetcode: Merge Intervals

Maximum size of square sub matrix with all 1’s in a binary matrix

Sequence Finder Dynamic Programming

TicTacToe Game As Asked in Flipkart

Top 10 Interviews Techniqes for Campus Interview in IIT NIT BITS for MTech

Count Possible Decodings of a given Digit Sequence

Find and print longest consecutive number sequence in a given sequence in O(n)

There are N nuts and N bolts, u have to find all the pairs of nuts and bolts in minimum no. of iteration

Given Set of words or A String find whether chain is possible from these words or not

Trie Dictionary

Find an index i such that Arr [i] = i in array of n distinct integers sorted in ascending order.

Right view of Binary tree

Copyright © 2025 · Genesis Framework · WordPress · Log in