Friday, May 26, 2017

Add Digits

Problem statement

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
Could you do it without any loop/recursion in O(1) runtime?

Wednesday, May 24, 2017

Simple Angular Route

Create an angular component

  • Create a component.ts
  • Create an html file
  • Import angular core modules
  • Define metadata for the component using @Component
  • Create a new class,specify the properties of a class and export the class

Wednesday, May 17, 2017

Length of last Word


Problem Statement

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.
Note: A word is defined as a character sequence consists of non-space characters only.
Link to GitHub : Code

Given s = "Prathap Kudupu",
return 6.

Solution

  • The result  is the difference between the length of the string and and index of  empty space
     public static int optimum(String s)
 {
  return s.trim().length()-s.trim().indexOf(' ')-1;
 }

Reverse Vowels of a String

Problem Statement

Write a function that takes a string as input and reverse only the vowels of a string.

Link to GitHub :Code

Tuesday, May 16, 2017

Student Attendance Record I

Problem Statement

You are given a string representing an attendance record for a student. The record only contains the following three characters:
  1. 'A' : Absent.
  2. 'L' : Late.
  3. 'P' : Present.
A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
You need to return whether the student could be rewarded according to his attendance record.

Link to GitHub:Code

Largest Uncommon Sequence 1


Problem Statement

Given a group of two strings, you need to find the longest uncommon subsequence of this group of two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings.
subsequence is a sequence that can be derived from one sequence by deleting some characters without changing the order of the remaining elements. Trivially, any string is a subsequence of itself and an empty string is a subsequence of any string.
The input will be two strings, and the output needs to be the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.
Link to GitHub Java: Code
Link to GitHub JavaScript : Code

Valid Palindrome


Problem Statement

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Example:


"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.

Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.

Solution

Link to GitHub python:Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def isPalindrome(s):
    head,tail=0,len(s)-1
    while head < tail:
        while head <tail and not s[head].isalnum():
            head+=1
        while head <tail and not s[tail].isalnum():
            tail-=1
        if s[head].lower() != s[tail].lower():
            return False
        head+=1; tail-=1
    return True
isPalindrome("bob bob")
Link to GitHub Java  :Code
Link to GitHub JavaScript:  Code


  • We need 2 pointers head and tail
  • Head is the initial position and tail is the last position in the array
  • Increment the header if we do not find valid character .Decrement the tail if we do not find the valid character.
  • Loop through the array till we find tail is greater than head
  • Return false if header character is not equal to tail
public static boolean get(String str)
 {
  //if string is empty return true
  if(str.isEmpty())
  {
   return true;
  }
  int head =0, tail=str.length()-1;
  //loop  through the strings to find if it is a valid palindrome
   while(head <= tail)
   {
    //Increment the header if the character isLetter or digit
    if(!Character.isLetterOrDigit(str.charAt(head))){
     head++;
    }
    //Decrement the tail if the character isLetter or digit
    else if(!Character.isLetterOrDigit(str.charAt(tail))){
     tail--;
    }
    else {
       if(Character.toLowerCase(str.charAt(head)) 
                                     !=Character.toLowerCase(str.charAt(tail)))
       {
        return false;
       }
       head++;
       tail--;
     
    }
   }
  return true;
 }

Needle in HayStack

Problem Statement

Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Link to GitHub JavaCode
Link to GitHub JavaScript Code

Monday, May 15, 2017

Docusign emails are stolen and are used in phishing campaign






If you received any message from this email then be cautious.Your information has been compromised. Please do do not click on the link or download any document.

Mail you might receive

"Dear Applicant~

Valid Parentheses


Problem Statement

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Link to GitHub :Code

Reverse a string III



Problem Statement

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving white space and initial word order.

Link to GitHub : Code


Longest Common Prefix

Problem statement

Write a function to find the longest common prefix string among an array of strings

Link to GitHub : code


Roman to Integer


Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.

Link to GitHubCode

Sunday, May 14, 2017

Detect Capitol


Given a word, you need to judge whether the usage of capitals in it is right or not.
We define the usage of capitals in a word to be right when one of the following cases holds:
  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.

Link to GitHub :code

Number of Segments in a String


Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:

Input: "Hello, my name is John"
Output: 5

Link to GitHub :Code

Saturday, May 13, 2017

Reverse String II


  • Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. 
  • If there are less than k characters left, reverse all of them. 
  • If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Link to Git HubCode

Friday, May 12, 2017

Two Sum II - Input array is sorted



Problem Statement

Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution and you may not use the same element twice.

Input and Output

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

Link to GitHub :Code

Thursday, May 11, 2017

Maximum SubArray


Problem Statement

Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6

Link to GitHubCode


Majority element


Problem Statement

Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
       I/P : 3 3 4 2 4 4 2 4 4
       O/P : 4 

       I/P : 3 3 4 2 4 4 2 4
       O/P : NONE
Link to GitHub: Code

Rotate Array


Problem Statement

Rotate an array of n elements to the right by k steps.
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]
Link to GitHub: Code

Wednesday, May 10, 2017

Pascal's Triangle



Problem Statement


Given numRows, generate the first numRows of Pascal's triangle.

Link to GitHub :Code
For example, given numRows = 5,

Return

Third Maximum Number



Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Link to GitHub :Code

Pascal's Triangle II


Problem Statement


Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
Link to GitHub :link

Search Insert Position

Problem statement

Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.

Link to GithubCode
Example
[1,3,5,6], 5  2
[1,3,5,6], 2  1
[1,3,5,6], 7  4
[1,3,5,6], 0  0

Thursday, May 4, 2017

CallBack Functions




CallBack Function

  • Passed as an argument to another function, and,
  • is invoked after some kind of event.
  • Once its parent function completes, the function passed as an argument is then called

Top 4 important features of angular 2

Wednesday, May 3, 2017

Testing tools for WCAG accessibility test



Contrast ratio checker

The contrast ratio is a property of a display system, defined as the ratio of the luminance of the brightest color (white) to that of the darkest color (black) that the system is capable of producing. A high contrast ratio is a desired aspect of any display

We enter background color on ther right and text color on the right. It would give a text value
It gives the contrast ratio for those colors
Guidelines specifies 4.5 to 1 min contrast ratios for text sizes 14 pt bold =18.7 pixel

Link to Contrast ratio checker tool

Tips for making your web site WCAG compliant

These are some of the tips for making sure that our site is WCAG compliant

  • Label  all form elements
  • Provide alternate text for non-text content. This would help people who are blind with assistance technology which can announce to them.People who are blind can use screen readers like 
    • VoiceOver is inbuilt in mac
    • NVDA and JAWS has to be downloaded for windows
    • Chrome browser has chromevox
  • We need to make sure that color contrast meets minimum thresholds. There are guidelines for threshold
  • Add dots to end of important color message
  • Add captions to audio content
  • Based on the guidelines we need to make sure that we have the ratio for the contrast for all the colors in our website
  • Use aria guidelines as mentioned in the link http://www.prathapkudupublog.com/2017/05/overview-of-wcag.html

        

Overview of WCAG



WCAG is a series of web accessibility guidelines published  by W3C.

There are 3 levels of WCAG 2.0 specifications.

1. WCAG Single Level A,
2. WCAG Double Level A.
3. WCAG Triple Level A

Four important features of Angular 1


Tuesday, May 2, 2017

Tricky double problem


In one of the interview I was asked how to eliminate tricky double in an array.I have not heard about tricky double.

Tricky double means
" 1, 2, 8, 9, 1, 2 , 3, 2 " . In this example what we notice is that 1, 2 is a  tricky double. However 2 is repeated twice it is not a tricky double.

JavaScript pass by reference and pass by value




Pass by value

When we change the value in the inner scope a copy of the value is created, This is known as pass by value.

Promises in JavaScript


Promises

Promises in JavaScript are like promises in real life
  • A promise can only succeed or fail once
  • It cannot succeed or fail twice, neither can it switch from success to failure or vice versa
  • If a promise has succeeded or failed, we later add a success/failure callback. The correct callback will be called, even though the event took place earlier

Closures in JavaScript


A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.